Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///By: Nathan Rumsey 2023
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.InputSystem;
- using Unity.Netcode;
- using Cinemachine;
- [HelpURL("https://pastebin.com/u/Dieton")]
- [RequireComponent(typeof(CharacterController))]
- public class Teacher : NetworkBehaviour
- {
- //[][]== VARIABLES ==[][]//
- #region Variables
- //-- Player Variables --//
- private float realTime;
- //-- Input Variables --//
- private PlayerInput playerInput;
- private MyInputActionControls myInputActions;
- private MyInputActionControls myInputActionsGet
- {
- get
- {
- if (myInputActions != null) { return myInputActions; }
- return myInputActions = new MyInputActionControls();
- }
- }
- //-- player movement --//
- [SerializeField, Header("Player Movement")]
- private float playerMoveSpeed = 2.0f;
- //-- Camera --//
- [SerializeField] private Transform cameraLookFromPoint;
- private Vector2 mouseAxis;
- private float lookSpeed = 50.5f;
- // -- Animation --//
- [SerializeField] private Animator thisAnimator;
- //-- charehcter --//
- private CharacterController characterController;
- private float gravity = 1.8f;
- //-- Network --//
- public GameManager gameManager;
- private NetworkObject playerNetworkObject;
- //-- Renderer --//
- [SerializeField] private SkinnedMeshRenderer[] playerSkinMeshRender;
- //---- User Interface ----//
- [SerializeField] private GameObject userInterface;
- [SerializeField] private GameObject studentNameUiPrefab;
- [SerializeField] private Transform studentListLocation;
- [SerializeField] private List<Student> students = new List<Student>();
- //[SerializeField] private List<GameObject> studentNameUiPrefabInList = new List<GameObject>();
- private GameObject[] studentsObj;
- //-- Debug --//
- [SerializeField] private bool playerDebugIO = false;
- #endregion
- //==== On Network Start ====//
- public override void OnNetworkSpawn()
- {
- gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
- if (!IsOwner)
- {
- return;
- }
- if (playerSkinMeshRender.Length > 0 && IsLocalPlayer)
- {
- for (int i = 0; i < playerSkinMeshRender.Length; i++)
- {
- playerSkinMeshRender[i].enabled = false;
- }
- }
- pDebugger(" - On Network Spawn");
- playerNetworkObject = GetComponent<NetworkObject>();
- PlayerInfoDebug();
- GameObject.Find("MainCamera").GetComponent<CinemachineVirtualCamera>().Follow = cameraLookFromPoint;
- PlayerInputSetup();
- }
- //==== Player Debug Info ====//
- private void PlayerInfoDebug()
- {
- pDebugger("====-- Player Type --====");
- pDebugger(name);
- if (IsServer) pDebugger("Is Server");
- if (IsHost) pDebugger("Is Host");
- if (IsClient) pDebugger("Is Client");
- if (IsLocalPlayer) pDebugger("Is Local Player");
- if (IsOwner) pDebugger("Is Owner");
- if (IsSpawned) pDebugger("Is Spawned");
- if (isActiveAndEnabled) pDebugger("Is Active And Enabled");
- pDebugger("====-- Network Object info --====");
- pDebugger("NeyObj - InstanceID ID" + playerNetworkObject.GetInstanceID());
- pDebugger("NeyObj - Hash Code" + playerNetworkObject.GetHashCode());
- pDebugger("NeyObj - Owner Client Id" + playerNetworkObject.OwnerClientId);
- pDebugger("NeyObj - Network Object Id" + playerNetworkObject.NetworkObjectId);
- pDebugger("====-- --====");
- }
- //[][]== INPUT SYSTEM ==[][]
- #region InputSystem
- //==== Input Sytem ====//
- public void PlayerInputSetup()
- {
- characterController = GetComponent<CharacterController>();
- playerInput = GetComponent<PlayerInput>();
- myInputActions = new MyInputActionControls();
- myInputActionsGet.Enable();
- myInputActionsGet.Player.Jump.performed += ctx => Jump();
- myInputActionsGet.Player.UserList.performed += ctx => UserList();
- }
- private void OnEnable() => myInputActionsGet.Enable();
- private void OnDisable() => myInputActionsGet.Disable();
- #endregion
- //==== Jump ====//
- public void Jump()
- {
- }
- //==== UserList ====//
- private void UserList() {
- userInterface.SetActive(true);
- students.Clear();
- //tags students teachers
- /*
- if (studentsObj.Length > 0)
- {
- foreach (GameObject item in studentsObj)
- {
- Destroy(item.gameObject);
- }
- }
- */
- studentsObj = GameObject.FindGameObjectsWithTag("students");
- foreach (GameObject item in studentsObj)
- {
- students.Add(item.GetComponent<Student>());
- GameObject studentNameClone = Instantiate(studentNameUiPrefab);
- studentNameClone.transform.SetParent(studentListLocation);
- studentNameClone.transform.position = new Vector3(0,0,0);
- //studentListLocation
- studentNameClone.GetComponent<TeacherStudentListNameUi>().studentNameText.text = item.name;
- }
- }
- public void MoveStudents() {
- // gameManager.moveStudent.Value = !gameManager.moveStudent.Value;
- }
- //==== MoveStudentsButtonServerRpc ====//
- [ServerRpc(RequireOwnership = false)]
- public void MoveStudentsButtonServerRpc() {
- Debug.Log("ServerRpc - Move Student Ran");
- /*
- if (students.Count < 0)
- {
- return;
- }
- */
- /*
- for (int i = 0; i < students.Count; i++)
- {
- students[i].TeleportPlayerToChair();
- }
- */
- MoveStudentsButtonClientRpc();
- }
- [ClientRpc]
- public void MoveStudentsButtonClientRpc() {
- Debug.Log("ClientRpc - Move Student Ran");
- for (int i = 0; i < students.Count; i++)
- {
- students[i].TeleportPlayerToChair();
- }
- }
- //==== Trigger ====//
- public void OnTriggerEnter(Collider other)
- {
- if (other.gameObject.name == "ClassroomTeleporter")
- {
- characterController.enabled = false;
- transform.position = GameObject.Find("TeacherSpawnPosition_0").transform.position;
- characterController.enabled = true;
- }
- if (other.gameObject.name == "hallwayTeleporter")
- {
- characterController.enabled = false;
- transform.position = GameObject.Find("studenSpawnPosition_0").transform.position;
- characterController.enabled = true;
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (!IsOwner)
- {
- return;
- }
- realTime = Time.deltaTime;
- PlayerMove();
- }
- //==== Player Move ====//
- public void PlayerMove()
- {
- //---- Player Movement ----//
- Vector2 PlayerMovementInputVector = myInputActionsGet.Player.Move.ReadValue<Vector2>();
- Vector2 playerMouseMoveInputVector = myInputActionsGet.Player.Look.ReadValue<Vector2>();
- float moveX = PlayerMovementInputVector.x;// _input.move.x
- float moveZ = PlayerMovementInputVector.y;//_input.move.y
- Vector3 direction = transform.TransformDirection(PlayerMovementInputVector.x, -gravity, PlayerMovementInputVector.y) * playerMoveSpeed;
- characterController.Move(direction * realTime);
- //---- Player Animation ----//
- thisAnimator.SetFloat("Speed", PlayerMovementInputVector.y);
- //---- Camera movement ----//
- if (mouseAxis.y > 180)
- {
- mouseAxis.y = 180;
- }
- if (mouseAxis.y < 0)
- {
- mouseAxis.y = 0;
- }
- mouseAxis.x += playerMouseMoveInputVector.x * lookSpeed * realTime;
- mouseAxis.y += playerMouseMoveInputVector.y * lookSpeed * realTime;
- float pitchRotation = ClampAngle(mouseAxis.y, 70, -70);
- transform.Rotate(0, playerMouseMoveInputVector.x * realTime * lookSpeed, 0);
- cameraLookFromPoint.rotation = Quaternion.Euler(pitchRotation, transform.eulerAngles.y, transform.eulerAngles.z);
- }
- //==== Clamp Angle ====//
- private static float ClampAngle(float angle, float max, float min)
- {
- if (angle < -360f) angle += 360f;
- if (angle > 360f) angle -= 360f;
- return Mathf.Clamp(angle, min, max);
- }
- //==== Player Debugger ====//
- public void pDebugger(object debugObj)
- {
- if (playerDebugIO) Debug.Log($"Player {0}: { debugObj}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement