Advertisement
Dieton

Teacher

Jun 23rd, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.74 KB | Gaming | 0 0
  1. ///By: Nathan Rumsey 2023
  2.  
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.InputSystem;
  7. using Unity.Netcode;
  8. using Cinemachine;
  9.  
  10. [HelpURL("https://pastebin.com/u/Dieton")]
  11. [RequireComponent(typeof(CharacterController))]
  12. public class Teacher : NetworkBehaviour
  13. {
  14.     //[][]== VARIABLES ==[][]//
  15.     #region Variables
  16.  
  17.     //-- Player Variables --//
  18.     private float realTime;
  19.  
  20.     //-- Input Variables --//
  21.     private PlayerInput playerInput;
  22.     private MyInputActionControls myInputActions;
  23.     private MyInputActionControls myInputActionsGet
  24.     {
  25.         get
  26.         {
  27.             if (myInputActions != null) { return myInputActions; }
  28.             return myInputActions = new MyInputActionControls();
  29.         }
  30.     }
  31.  
  32.     //-- player movement --//
  33.     [SerializeField, Header("Player Movement")]
  34.     private float playerMoveSpeed = 2.0f;
  35.  
  36.     //-- Camera --//
  37.     [SerializeField] private Transform cameraLookFromPoint;
  38.     private Vector2 mouseAxis;
  39.     private float lookSpeed = 50.5f;
  40.  
  41.     // -- Animation --//
  42.     [SerializeField] private Animator thisAnimator;
  43.  
  44.     //-- charehcter --//
  45.     private CharacterController characterController;
  46.     private float gravity = 1.8f;
  47.  
  48.     //-- Network --//
  49.     public GameManager gameManager;
  50.     private NetworkObject playerNetworkObject;
  51.  
  52.     //-- Renderer --//
  53.     [SerializeField] private SkinnedMeshRenderer[] playerSkinMeshRender;
  54.  
  55.     //---- User Interface ----//
  56.     [SerializeField] private GameObject userInterface;
  57.     [SerializeField] private GameObject studentNameUiPrefab;
  58.     [SerializeField] private Transform studentListLocation;
  59.     [SerializeField] private List<Student> students = new List<Student>();
  60.     //[SerializeField] private List<GameObject> studentNameUiPrefabInList = new List<GameObject>();
  61.     private GameObject[] studentsObj;
  62.  
  63.     //-- Debug --//
  64.     [SerializeField] private bool playerDebugIO = false;
  65.  
  66.     #endregion
  67.  
  68.     //==== On Network Start ====//
  69.     public override void OnNetworkSpawn()
  70.     {
  71.         gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
  72.  
  73.         if (!IsOwner)
  74.         {
  75.             return;
  76.         }
  77.  
  78.         if (playerSkinMeshRender.Length > 0 && IsLocalPlayer)
  79.         {
  80.             for (int i = 0; i < playerSkinMeshRender.Length; i++)
  81.             {
  82.                 playerSkinMeshRender[i].enabled = false;
  83.             }
  84.         }
  85.  
  86.         pDebugger(" - On Network Spawn");
  87.         playerNetworkObject = GetComponent<NetworkObject>();
  88.  
  89.         PlayerInfoDebug();
  90.  
  91.         GameObject.Find("MainCamera").GetComponent<CinemachineVirtualCamera>().Follow = cameraLookFromPoint;
  92.         PlayerInputSetup();
  93.     }
  94.  
  95.     //==== Player Debug Info ====//
  96.     private void PlayerInfoDebug()
  97.     {
  98.         pDebugger("====-- Player Type --====");
  99.         pDebugger(name);
  100.         if (IsServer) pDebugger("Is Server");
  101.         if (IsHost) pDebugger("Is Host");
  102.         if (IsClient) pDebugger("Is Client");
  103.         if (IsLocalPlayer) pDebugger("Is Local Player");
  104.         if (IsOwner) pDebugger("Is Owner");
  105.         if (IsSpawned) pDebugger("Is Spawned");
  106.         if (isActiveAndEnabled) pDebugger("Is Active And Enabled");
  107.         pDebugger("====-- Network Object info --====");
  108.         pDebugger("NeyObj - InstanceID ID" + playerNetworkObject.GetInstanceID());
  109.         pDebugger("NeyObj - Hash Code" + playerNetworkObject.GetHashCode());
  110.         pDebugger("NeyObj - Owner Client Id" + playerNetworkObject.OwnerClientId);
  111.         pDebugger("NeyObj - Network Object Id" + playerNetworkObject.NetworkObjectId);
  112.         pDebugger("====--    --====");
  113.  
  114.     }
  115.    
  116.     //[][]== INPUT SYSTEM ==[][]
  117.     #region InputSystem
  118.  
  119.     //==== Input Sytem ====//
  120.     public void PlayerInputSetup()
  121.     {
  122.         characterController = GetComponent<CharacterController>();
  123.         playerInput = GetComponent<PlayerInput>();
  124.         myInputActions = new MyInputActionControls();
  125.         myInputActionsGet.Enable();
  126.         myInputActionsGet.Player.Jump.performed += ctx => Jump();
  127.         myInputActionsGet.Player.UserList.performed += ctx => UserList();
  128.     }
  129.  
  130.     private void OnEnable() => myInputActionsGet.Enable();
  131.  
  132.     private void OnDisable() => myInputActionsGet.Disable();
  133.  
  134.     #endregion
  135.  
  136.     //==== Jump ====//
  137.     public void Jump()
  138.     {
  139.  
  140.     }
  141.  
  142.     //==== UserList ====//
  143.     private void UserList() {
  144.         userInterface.SetActive(true);
  145.         students.Clear();
  146.         //tags students teachers
  147.        
  148.         /*
  149.         if (studentsObj.Length > 0)
  150.         {
  151.             foreach (GameObject item in studentsObj)
  152.             {
  153.                 Destroy(item.gameObject);
  154.             }
  155.         }
  156.         */
  157.  
  158.         studentsObj = GameObject.FindGameObjectsWithTag("students");
  159.  
  160.         foreach (GameObject item in studentsObj)
  161.         {
  162.             students.Add(item.GetComponent<Student>());
  163.             GameObject studentNameClone = Instantiate(studentNameUiPrefab);
  164.             studentNameClone.transform.SetParent(studentListLocation);
  165.             studentNameClone.transform.position = new Vector3(0,0,0);
  166.             //studentListLocation
  167.             studentNameClone.GetComponent<TeacherStudentListNameUi>().studentNameText.text = item.name;
  168.         }
  169.     }
  170.  
  171.     public void MoveStudents() {
  172.        // gameManager.moveStudent.Value = !gameManager.moveStudent.Value;
  173.     }
  174.  
  175.     //==== MoveStudentsButtonServerRpc ====//
  176.     [ServerRpc(RequireOwnership = false)]
  177.     public void MoveStudentsButtonServerRpc() {
  178.  
  179.         Debug.Log("ServerRpc - Move Student Ran");
  180.  
  181.         /*
  182.         if (students.Count < 0)
  183.         {
  184.             return;
  185.         }
  186.         */
  187.         /*
  188.         for (int i = 0; i < students.Count; i++)
  189.         {
  190.             students[i].TeleportPlayerToChair();
  191.         }
  192.         */
  193.  
  194.         MoveStudentsButtonClientRpc();
  195.     }
  196.  
  197.     [ClientRpc]
  198.     public void MoveStudentsButtonClientRpc() {
  199.  
  200.         Debug.Log("ClientRpc - Move Student Ran");
  201.  
  202.         for (int i = 0; i < students.Count; i++)
  203.         {
  204.             students[i].TeleportPlayerToChair();
  205.         }
  206.     }
  207.  
  208.     //==== Trigger ====//
  209.     public void OnTriggerEnter(Collider other)
  210.     {
  211.         if (other.gameObject.name == "ClassroomTeleporter")
  212.         {
  213.             characterController.enabled = false;
  214.  
  215.             transform.position = GameObject.Find("TeacherSpawnPosition_0").transform.position;
  216.  
  217.             characterController.enabled = true;
  218.         }
  219.  
  220.         if (other.gameObject.name == "hallwayTeleporter")
  221.         {
  222.             characterController.enabled = false;
  223.  
  224.             transform.position = GameObject.Find("studenSpawnPosition_0").transform.position;
  225.  
  226.             characterController.enabled = true;
  227.         }
  228.     }
  229.  
  230.     // Update is called once per frame
  231.     void Update()
  232.     {
  233.         if (!IsOwner)
  234.         {
  235.             return;
  236.         }
  237.  
  238.         realTime = Time.deltaTime;
  239.  
  240.         PlayerMove();
  241.     }
  242.  
  243.     //==== Player Move ====//
  244.     public void PlayerMove()
  245.     {
  246.         //---- Player Movement ----//
  247.         Vector2 PlayerMovementInputVector = myInputActionsGet.Player.Move.ReadValue<Vector2>();
  248.         Vector2 playerMouseMoveInputVector = myInputActionsGet.Player.Look.ReadValue<Vector2>();
  249.         float moveX = PlayerMovementInputVector.x;// _input.move.x
  250.         float moveZ = PlayerMovementInputVector.y;//_input.move.y
  251.         Vector3 direction = transform.TransformDirection(PlayerMovementInputVector.x, -gravity, PlayerMovementInputVector.y) * playerMoveSpeed;
  252.  
  253.         characterController.Move(direction * realTime);
  254.  
  255.         //---- Player Animation ----//
  256.         thisAnimator.SetFloat("Speed", PlayerMovementInputVector.y);
  257.  
  258.         //---- Camera movement ----//
  259.         if (mouseAxis.y > 180)
  260.         {
  261.             mouseAxis.y = 180;
  262.         }
  263.  
  264.         if (mouseAxis.y < 0)
  265.         {
  266.             mouseAxis.y = 0;
  267.         }
  268.  
  269.         mouseAxis.x += playerMouseMoveInputVector.x * lookSpeed * realTime;
  270.         mouseAxis.y += playerMouseMoveInputVector.y * lookSpeed * realTime;
  271.  
  272.         float pitchRotation = ClampAngle(mouseAxis.y, 70, -70);
  273.  
  274.         transform.Rotate(0, playerMouseMoveInputVector.x * realTime * lookSpeed, 0);
  275.         cameraLookFromPoint.rotation = Quaternion.Euler(pitchRotation, transform.eulerAngles.y, transform.eulerAngles.z);
  276.     }
  277.  
  278.     //==== Clamp Angle ====//
  279.     private static float ClampAngle(float angle, float max, float min)
  280.     {
  281.         if (angle < -360f) angle += 360f;
  282.         if (angle > 360f) angle -= 360f;
  283.         return Mathf.Clamp(angle, min, max);
  284.     }
  285.  
  286.     //==== Player Debugger ====//
  287.     public void pDebugger(object debugObj)
  288.     {
  289.         if (playerDebugIO) Debug.Log($"Player {0}: { debugObj}");
  290.     }
  291. }
  292.  
Tags: Unity Netcode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement