Advertisement
Dieton

Student

Jun 23rd, 2023
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.09 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 Student : 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 MyInputActions myInputActions;
  23.     private MyInputActions myInputActionsGet
  24.     {
  25.         get
  26.         {
  27.             if (myInputActions != null) { return myInputActions; }
  28.             return myInputActions = new MyInputActions();
  29.         }
  30.     }
  31.  
  32.    
  33.  
  34.     //-- player movement --//
  35.     [SerializeField, Header("Player Movement")]
  36.     private float playerMoveSpeed = 2.0f;
  37.  
  38.     //-- Camera --//
  39.     [SerializeField] private Transform cameraLookFromPoint;
  40.     private Vector2 mouseAxis;
  41.     private float lookSpeed = 200.5f;
  42.  
  43.     // -- Animation --//
  44.     [SerializeField] private Animator thisAnimator;
  45.  
  46.     //-- charehcter --//
  47.     public CharacterController characterController;
  48.     private float gravity = 1.8f;
  49.  
  50.     //-- Teleport Chair --//
  51.     private GameObject teleportChairlocation;
  52.  
  53.     //-- Network --//
  54.     private NetworkObject playerNetworkObject;
  55.  
  56.     //-- Renderer --//
  57.     [SerializeField] private SkinnedMeshRenderer[] playerSkinMeshRender;
  58.  
  59.     //-- Debug --//
  60.     [SerializeField] private bool playerDebugIO = false;
  61.  
  62.     #endregion
  63.  
  64.     //==== On Network Start ====//
  65.     public override void OnNetworkSpawn()
  66.     {
  67.         if (!IsOwner)
  68.         {
  69.             return;
  70.         }
  71.  
  72.         if (playerSkinMeshRender.Length > 0 && IsLocalPlayer)
  73.         {
  74.             for (int i = 0; i < playerSkinMeshRender.Length; i++)
  75.             {
  76.                 playerSkinMeshRender[i].enabled = false;
  77.             }
  78.         }
  79.  
  80.         pDebugger(" - On Network Spawn");
  81.         playerNetworkObject = GetComponent<NetworkObject>();
  82.  
  83.         PlayerInfoDebug();
  84.  
  85.         GameObject.Find("MainCamera").GetComponent<CinemachineVirtualCamera>().Follow = cameraLookFromPoint;
  86.         PlayerInputSetup();
  87.     }
  88.  
  89.     //==== Player Debug Info ====//
  90.     private void PlayerInfoDebug()
  91.     {
  92.         pDebugger("====-- Player Type --====");
  93.         pDebugger(name);
  94.         if (IsServer) pDebugger("Is Server");
  95.         if (IsHost) pDebugger("Is Host");
  96.         if (IsClient) pDebugger("Is Client");
  97.         if (IsLocalPlayer) pDebugger("Is Local Player");
  98.         if (IsOwner) pDebugger("Is Owner");
  99.         if (IsSpawned) pDebugger("Is Spawned");
  100.         if (isActiveAndEnabled) pDebugger("Is Active And Enabled");
  101.         pDebugger("====-- Network Object info --====");
  102.         pDebugger("NeyObj - InstanceID ID" + playerNetworkObject.GetInstanceID());
  103.         pDebugger("NeyObj - Hash Code" + playerNetworkObject.GetHashCode());
  104.         pDebugger("NeyObj - Owner Client Id" + playerNetworkObject.OwnerClientId);
  105.         pDebugger("NeyObj - Network Object Id" + playerNetworkObject.NetworkObjectId);
  106.         pDebugger("====--    --====");
  107.  
  108.     }
  109.  
  110.     //[][]== INPUT SYSTEM ==[][]
  111.     #region InputSystem
  112.  
  113.     //==== Input Sytem ====//
  114.     public void PlayerInputSetup()
  115.     {
  116.         characterController = GetComponent<CharacterController>();
  117.         playerInput = GetComponent<PlayerInput>();
  118.         myInputActions = new MyInputActions();
  119.         myInputActionsGet.Enable();
  120.         myInputActionsGet.Player.Jump.performed += ctx => Jump();
  121.     }
  122.  
  123.     private void OnEnable() => myInputActionsGet.Enable();
  124.  
  125.     private void OnDisable() => myInputActionsGet.Disable();
  126.  
  127.     #endregion
  128.  
  129.     //==== Jump ====//
  130.     public void Jump()
  131.     {
  132.         if (!characterController.enabled)
  133.         {
  134.             teleportChairlocation.tag = "sat";
  135.             teleportChairlocation = null;
  136.             characterController.enabled = true;
  137.         }
  138.     }
  139.  
  140.     //==== StudentTeleportAction ====//
  141.     public void TeleportPlayerToChair() {
  142.         Debug.Log("student is atampting to sit in chair");
  143.  
  144.         if (teleportChairlocation)
  145.         {
  146.             return;
  147.         }
  148.  
  149.         teleportChairlocation = GameObject.FindGameObjectWithTag("sit");
  150.         characterController.enabled = false;
  151.         transform.position = teleportChairlocation.transform.position;
  152.         teleportChairlocation.tag = "sat";
  153.  
  154.         Debug.Log("student sat in chair");
  155.     }
  156.  
  157.     public void OnTriggerEnter(Collider other)
  158.     {
  159.         if (other.gameObject.name == "ClassroomTeleporter")
  160.         {
  161.             characterController.enabled = false;
  162.  
  163.             transform.position = GameObject.Find("TeacherSpawnPosition_0").transform.position;
  164.  
  165.             characterController.enabled = true;
  166.         }
  167.  
  168.         if (other.gameObject.name == "hallwayTeleporter")
  169.         {
  170.             characterController.enabled = false;
  171.  
  172.             transform.position = GameObject.Find("studenSpawnPosition_0").transform.position;
  173.  
  174.             characterController.enabled = true;
  175.         }
  176.     }
  177.  
  178.     // Update is called once per frame
  179.     void Update()
  180.     {
  181.        
  182.  
  183.         if (!IsOwner)
  184.         {
  185.             return;
  186.         }
  187.  
  188.         realTime = Time.deltaTime;
  189.  
  190.         PlayerMove();
  191.     }
  192.  
  193.     //==== Player Move ====//
  194.     public void PlayerMove()
  195.     {
  196.         //---- Player Movement ----//
  197.         Vector2 PlayerMovementInputVector = myInputActionsGet.Player.Move.ReadValue<Vector2>();
  198.         Vector2 playerMouseMoveInputVector = myInputActionsGet.Player.Look.ReadValue<Vector2>();
  199.         float moveX = PlayerMovementInputVector.x;// _input.move.x
  200.         float moveZ = PlayerMovementInputVector.y;//_input.move.y
  201.         Vector3 direction = transform.TransformDirection(PlayerMovementInputVector.x, -gravity, PlayerMovementInputVector.y) * playerMoveSpeed;
  202.  
  203.         characterController.Move(direction * realTime);
  204.  
  205.         //---- Player Animation ----//
  206.         thisAnimator.SetFloat("Speed", PlayerMovementInputVector.y);
  207.  
  208.         //---- Camera movement ----//
  209.         if (mouseAxis.y > 180)
  210.         {
  211.             mouseAxis.y = 180;
  212.         }
  213.  
  214.         if (mouseAxis.y < 0)
  215.         {
  216.             mouseAxis.y = 0;
  217.         }
  218.  
  219.         mouseAxis.x += playerMouseMoveInputVector.x * lookSpeed * realTime;
  220.         mouseAxis.y += playerMouseMoveInputVector.y * lookSpeed * realTime;
  221.  
  222.         float pitchRotation = ClampAngle(mouseAxis.y, 70, -70);
  223.  
  224.         transform.Rotate(0, playerMouseMoveInputVector.x * realTime * lookSpeed, 0);
  225.         cameraLookFromPoint.rotation = Quaternion.Euler(pitchRotation, transform.eulerAngles.y, transform.eulerAngles.z);
  226.     }
  227.  
  228.     //==== Clamp Angle ====//
  229.     private static float ClampAngle(float angle, float max, float min)
  230.     {
  231.         if (angle < -360f) angle += 360f;
  232.         if (angle > 360f) angle -= 360f;
  233.         return Mathf.Clamp(angle, min, max);
  234.     }
  235.  
  236.     //==== Player Debugger ====//
  237.     public void pDebugger(object debugObj)
  238.     {
  239.         if (playerDebugIO) Debug.Log($"Player {0}: { debugObj}");
  240.     }
  241. }
  242.  
Tags: Unity Netcode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement