PlayerController.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. characterController.enabled = false;
  17. GetComponent<Rigidbody>().isKinematic = true;
  18. GetComponent<NetworkTransform>().clientAuthority = true;
  19. }
  20. public override void OnStartLocalPlayer()
  21. {
  22. characterController.enabled = true;
  23. }
  24. [Header("Movement Settings")]
  25. public float moveSpeed = 8f;
  26. public float turnSensitivity = 5f;
  27. public float maxTurnSpeed = 100f;
  28. [Header("Diagnostics")]
  29. public float horizontal;
  30. public float vertical;
  31. public float turn;
  32. public float jumpSpeed;
  33. public bool isGrounded = true;
  34. public bool isFalling;
  35. public Vector3 velocity;
  36. void Update()
  37. {
  38. if (!isLocalPlayer || characterController == null || !characterController.enabled)
  39. return;
  40. horizontal = Input.GetAxis("Horizontal");
  41. vertical = Input.GetAxis("Vertical");
  42. // Q and E cancel each other out, reducing the turn to zero
  43. if (Input.GetKey(KeyCode.Q))
  44. turn = Mathf.MoveTowards(turn, -maxTurnSpeed, turnSensitivity);
  45. if (Input.GetKey(KeyCode.E))
  46. turn = Mathf.MoveTowards(turn, maxTurnSpeed, turnSensitivity);
  47. if (Input.GetKey(KeyCode.Q) && Input.GetKey(KeyCode.E))
  48. turn = Mathf.MoveTowards(turn, 0, turnSensitivity);
  49. if (!Input.GetKey(KeyCode.Q) && !Input.GetKey(KeyCode.E))
  50. turn = Mathf.MoveTowards(turn, 0, turnSensitivity);
  51. if (isGrounded)
  52. isFalling = false;
  53. if ((isGrounded || !isFalling) && jumpSpeed < 1f && Input.GetKey(KeyCode.Space))
  54. {
  55. jumpSpeed = Mathf.Lerp(jumpSpeed, 1f, 0.5f);
  56. }
  57. else if (!isGrounded)
  58. {
  59. isFalling = true;
  60. jumpSpeed = 0;
  61. }
  62. }
  63. void FixedUpdate()
  64. {
  65. if (!isLocalPlayer || characterController == null || !characterController.enabled)
  66. return;
  67. transform.Rotate(0f, turn * Time.fixedDeltaTime, 0f);
  68. Vector3 direction = new Vector3(horizontal, jumpSpeed, vertical);
  69. direction = Vector3.ClampMagnitude(direction, 1f);
  70. direction = transform.TransformDirection(direction);
  71. direction *= moveSpeed;
  72. if (jumpSpeed > 0)
  73. characterController.Move(direction * Time.fixedDeltaTime);
  74. else
  75. characterController.SimpleMove(direction);
  76. isGrounded = characterController.isGrounded;
  77. velocity = characterController.velocity;
  78. }
  79. }
  80. }