PlayerController.cs 3.2 KB

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