PlayerController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. namespace Mirror.Examples.MultipleAdditiveScenes
  4. {
  5. [RequireComponent(typeof(CapsuleCollider))]
  6. [RequireComponent(typeof(CharacterController))]
  7. [RequireComponent(typeof(NetworkTransform))]
  8. [RequireComponent(typeof(Rigidbody))]
  9. public class PlayerController : NetworkBehaviour
  10. {
  11. public CharacterController characterController;
  12. void OnValidate()
  13. {
  14. if (characterController == null)
  15. characterController = GetComponent<CharacterController>();
  16. }
  17. void Start()
  18. {
  19. characterController.enabled = isLocalPlayer;
  20. }
  21. public override void OnStartLocalPlayer()
  22. {
  23. Camera.main.orthographic = false;
  24. Camera.main.transform.SetParent(transform);
  25. Camera.main.transform.localPosition = new Vector3(0f, 3f, -8f);
  26. Camera.main.transform.localEulerAngles = new Vector3(10f, 0f, 0f);
  27. }
  28. void OnDisable()
  29. {
  30. if (isLocalPlayer && Camera.main != null)
  31. {
  32. Camera.main.orthographic = true;
  33. Camera.main.transform.SetParent(null);
  34. SceneManager.MoveGameObjectToScene(Camera.main.gameObject, SceneManager.GetActiveScene());
  35. Camera.main.transform.localPosition = new Vector3(0f, 70f, 0f);
  36. Camera.main.transform.localEulerAngles = new Vector3(90f, 0f, 0f);
  37. }
  38. }
  39. [Header("Movement Settings")]
  40. public float moveSpeed = 8f;
  41. public float turnSensitivity = 5f;
  42. public float maxTurnSpeed = 150f;
  43. [Header("Diagnostics")]
  44. public float horizontal;
  45. public float vertical;
  46. public float turn;
  47. public float jumpSpeed;
  48. public bool isGrounded = true;
  49. public bool isFalling;
  50. public Vector3 velocity;
  51. void Update()
  52. {
  53. if (!isLocalPlayer || !characterController.enabled)
  54. return;
  55. horizontal = Input.GetAxis("Horizontal");
  56. vertical = Input.GetAxis("Vertical");
  57. // Q and E cancel each other out, reducing the turn to zero
  58. if (Input.GetKey(KeyCode.Q))
  59. turn = Mathf.MoveTowards(turn, -maxTurnSpeed, turnSensitivity);
  60. if (Input.GetKey(KeyCode.E))
  61. turn = Mathf.MoveTowards(turn, maxTurnSpeed, turnSensitivity);
  62. if (Input.GetKey(KeyCode.Q) && Input.GetKey(KeyCode.E))
  63. turn = Mathf.MoveTowards(turn, 0, turnSensitivity);
  64. if (!Input.GetKey(KeyCode.Q) && !Input.GetKey(KeyCode.E))
  65. turn = Mathf.MoveTowards(turn, 0, turnSensitivity);
  66. if (isGrounded)
  67. isFalling = false;
  68. if ((isGrounded || !isFalling) && jumpSpeed < 1f && Input.GetKey(KeyCode.Space))
  69. {
  70. jumpSpeed = Mathf.Lerp(jumpSpeed, 1f, 0.5f);
  71. }
  72. else if (!isGrounded)
  73. {
  74. isFalling = true;
  75. jumpSpeed = 0;
  76. }
  77. }
  78. void FixedUpdate()
  79. {
  80. if (!isLocalPlayer || characterController == null)
  81. return;
  82. transform.Rotate(0f, turn * Time.fixedDeltaTime, 0f);
  83. Vector3 direction = new Vector3(horizontal, jumpSpeed, vertical);
  84. direction = Vector3.ClampMagnitude(direction, 1f);
  85. direction = transform.TransformDirection(direction);
  86. direction *= moveSpeed;
  87. if (jumpSpeed > 0)
  88. characterController.Move(direction * Time.fixedDeltaTime);
  89. else
  90. characterController.SimpleMove(direction);
  91. isGrounded = characterController.isGrounded;
  92. velocity = characterController.velocity;
  93. }
  94. }
  95. }