PlayerController.cs 3.6 KB

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