Guest User

Untitled

a guest
Oct 20th, 2021
40
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Mirror;
  5.  
  6. [RequireComponent(typeof(CharacterController))]
  7.  
  8. public class PlayerController : NetworkBehaviour
  9. {
  10. public float walkingSpeed = 7.5f;
  11. public float runningSpeed = 11.5f;
  12. // public float sneakingSpeed = 4.2;
  13. public float jumpSpeed = 8.0f;
  14. public float gravity = 20.0f;
  15. public Camera playerCamera;
  16. public float lookSpeed = 2.0f;
  17. public float lookXLimit = 45.0f;
  18.  
  19. CharacterController characterController;
  20. Vector3 moveDirection = Vector3.zero;
  21. float rotationX = 0;
  22.  
  23. [HideInInspector]
  24. public bool canMove = true;
  25.  
  26. void Start()
  27. {
  28. characterController = GetComponent<CharacterController>();
  29.  
  30. // Lock cursor
  31. Cursor.lockState = CursorLockMode.Locked;
  32. Cursor.visible = false;
  33.  
  34. if (!isLocalPlayer)
  35. {
  36. playerCamera.gameObject.SetActive(false);
  37. }
  38. }
  39.  
  40. void Update()
  41. {
  42. if (!isLocalPlayer)
  43. {
  44. return;
  45. }
  46. // We are grounded, so recalculate move direction based on axes
  47. Vector3 forward = transform.TransformDirection(Vector3.forward);
  48. Vector3 right = transform.TransformDirection(Vector3.right);
  49. // Press Left Shift to run
  50. bool isRunning = Input.GetKey(KeyCode.LeftShift);
  51. float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
  52. float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
  53. float movementDirectionY = moveDirection.y;
  54. moveDirection = (forward * curSpeedX) + (right * curSpeedY);
  55.  
  56. // bool isRunning = Input.GetKey(KeyCode.LeftAlt);
  57.  
  58.  
  59. // if (Input.GetKey(KeyCode.LeftAlt))
  60.  
  61.  
  62. if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
  63. {
  64. moveDirection.y = jumpSpeed;
  65. }
  66. else
  67. {
  68. moveDirection.y = movementDirectionY;
  69. }
  70.  
  71.  
  72. // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
  73. // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
  74. // as an acceleration (ms^-2)
  75. if (!characterController.isGrounded)
  76. {
  77. moveDirection.y -= gravity * Time.deltaTime;
  78. }
  79.  
  80. // Move the controller
  81. characterController.Move(moveDirection * Time.deltaTime);
  82.  
  83. // Player and Camera rotation
  84. if (canMove)
  85. {
  86. rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
  87. rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
  88. playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
  89. transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
  90. }
  91. }
  92. }
  93.  
RAW Paste Data