Advertisement
Guest User

playercontroller

a guest
Jul 7th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using UnityEditor.ShortcutManagement;
  5. using UnityEngine;
  6.  
  7. public class PlayerController : MonoBehaviour
  8. {
  9. public Transform viewPoint;
  10. public float mouseSensitivity = 1f;
  11. private float verticalRotStore;
  12. //Vertical Rotation Score
  13.  
  14. public bool invertLook;
  15.  
  16. public CharacterController charCon;
  17.  
  18.  
  19. private float activeMoveSpeed;
  20. public float moveSpeed = 5f, runSpeed = 8f;
  21. private Vector3 moveDir, movement;
  22.  
  23. private Vector2 mouseInput;
  24. private Camera cam;
  25.  
  26. public float jumpForce = 12f;
  27. public float gravityMod = 2.5f;
  28.  
  29. public Transform groundCheckPoint;
  30. private bool isGrounded;
  31. public LayerMask groundLayers;
  32.  
  33.  
  34.  
  35. // Start is called before the first frame update
  36. void Start()
  37. {
  38. Cursor.lockState = CursorLockMode.Locked;
  39.  
  40. cam = Camera.main;
  41. }
  42.  
  43. // Update is called once per frame
  44. void Update()
  45. {
  46. mouseInput = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * mouseSensitivity;
  47.  
  48. transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + mouseInput.x, transform.rotation.eulerAngles.z);
  49.  
  50. verticalRotStore += mouseInput.y;
  51. verticalRotStore = Mathf.Clamp(verticalRotStore, -60f, 60f);
  52.  
  53. if(invertLook)
  54. {
  55.  
  56.  
  57.  
  58. viewPoint.rotation = Quaternion.Euler(verticalRotStore, viewPoint.rotation.eulerAngles.y, viewPoint.rotation.eulerAngles.z);
  59. }
  60. else
  61. {
  62. viewPoint.rotation = Quaternion.Euler(-verticalRotStore, viewPoint.rotation.eulerAngles.y, viewPoint.rotation.eulerAngles.z);
  63. }
  64.  
  65.  
  66. moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
  67.  
  68. if(Input.GetKey(KeyCode.LeftShift))
  69. {
  70. activeMoveSpeed = runSpeed;
  71. }
  72. else
  73. {
  74. activeMoveSpeed = moveSpeed;
  75. }
  76.  
  77. float yVel = movement.y;
  78.  
  79. movement = ((transform.forward * moveDir.z) + (transform.right * moveDir.x)).normalized * activeMoveSpeed;
  80. movement.y = yVel;
  81.  
  82. if(charCon.isGrounded)
  83. {
  84. movement.y = 0f;
  85. }
  86.  
  87. charCon.Move( movement * Time.deltaTime);
  88.  
  89. movement.y += Physics.gravity.y * Time.deltaTime * gravityMod;
  90.  
  91.  
  92. isGrounded = Physics.Raycast(groundCheckPoint.position, Vector3.down, 0.25f, groundLayers);
  93. if(Input.GetButtonDown("Jump") && isGrounded)
  94. {
  95. movement.y = jumpForce;
  96. }
  97.  
  98.  
  99.  
  100. }
  101. private void LateUpdate()
  102. {
  103. cam.transform.position = viewPoint.position;
  104. cam.transform.rotation = viewPoint.rotation;
  105. }
  106.  
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement