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 Student : NetworkBehaviour
- {
- //[][]== VARIABLES ==[][]//
- #region Variables
- //-- Player Variables --//
- private float realTime;
- //-- Input Variables --//
- private PlayerInput playerInput;
- private MyInputActions myInputActions;
- private MyInputActions myInputActionsGet
- {
- get
- {
- if (myInputActions != null) { return myInputActions; }
- return myInputActions = new MyInputActions();
- }
- }
- //-- player movement --//
- [SerializeField, Header("Player Movement")]
- private float playerMoveSpeed = 2.0f;
- //-- Camera --//
- [SerializeField] private Transform cameraLookFromPoint;
- private Vector2 mouseAxis;
- private float lookSpeed = 200.5f;
- // -- Animation --//
- [SerializeField] private Animator thisAnimator;
- //-- charehcter --//
- public CharacterController characterController;
- private float gravity = 1.8f;
- //-- Teleport Chair --//
- private GameObject teleportChairlocation;
- //-- Network --//
- private NetworkObject playerNetworkObject;
- //-- Renderer --//
- [SerializeField] private SkinnedMeshRenderer[] playerSkinMeshRender;
- //-- Debug --//
- [SerializeField] private bool playerDebugIO = false;
- #endregion
- //==== On Network Start ====//
- public override void OnNetworkSpawn()
- {
- 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 MyInputActions();
- myInputActionsGet.Enable();
- myInputActionsGet.Player.Jump.performed += ctx => Jump();
- }
- private void OnEnable() => myInputActionsGet.Enable();
- private void OnDisable() => myInputActionsGet.Disable();
- #endregion
- //==== Jump ====//
- public void Jump()
- {
- if (!characterController.enabled)
- {
- teleportChairlocation.tag = "sat";
- teleportChairlocation = null;
- characterController.enabled = true;
- }
- }
- //==== StudentTeleportAction ====//
- public void TeleportPlayerToChair() {
- Debug.Log("student is atampting to sit in chair");
- if (teleportChairlocation)
- {
- return;
- }
- teleportChairlocation = GameObject.FindGameObjectWithTag("sit");
- characterController.enabled = false;
- transform.position = teleportChairlocation.transform.position;
- teleportChairlocation.tag = "sat";
- Debug.Log("student sat in chair");
- }
- 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