Advertisement
Guest User

Untitled

a guest
May 6th, 2021
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(CharacterController))]
  4.  
  5. public class TPSController : MonoBehaviour
  6. {
  7. public float speed = 7.5f;
  8. public float jumpSpeed = 8.0f;
  9. public float gravity = 20.0f;
  10. public Transform playerCameraParent;
  11. public float lookSpeed = 2.0f;
  12. public float lookXLimit = 60.0f;
  13.  
  14. CharacterController characterController;
  15. Vector3 moveDirection = Vector3.zero;
  16. Vector2 rotation = Vector2.zero;
  17.  
  18. [HideInInspector]
  19. public bool canMove = true;
  20.  
  21. void Start()
  22. {
  23. characterController = GetComponent<CharacterController>();
  24. rotation.y = transform.eulerAngles.y;
  25. }
  26.  
  27. void Update()
  28. {
  29. if (characterController.isGrounded)
  30. {
  31. // We are grounded, so calculate move direction based on axes
  32. Vector3 forward = transform.TransformDirection(Vector3.forward);
  33. Vector3 right = transform.TransformDirection(Vector3.right);
  34. float curSpeedX = canMove ? speed * Input.GetAxis("Vertical") : 0;
  35. float curSpeedY = canMove ? speed * Input.GetAxis("Horizontal") : 0;
  36. moveDirection = (forward * curSpeedX) + (right * curSpeedY);
  37.  
  38. if (Input.GetButton("Jump") && canMove)
  39. {
  40. moveDirection.y = jumpSpeed;
  41. }
  42. }
  43.  
  44. // Apply gravity.
  45. moveDirection.y -= gravity * Time.deltaTime;
  46.  
  47. // Move the controller
  48. characterController.Move(moveDirection * Time.deltaTime);
  49.  
  50. // Player and Camera rotation
  51. if (canMove)
  52. {
  53. rotation.y += Input.GetAxis("Mouse X") * lookSpeed;
  54. rotation.x += -Input.GetAxis("Mouse Y") * lookSpeed;
  55. rotation.x = Mathf.Clamp(rotation.x, -lookXLimit, lookXLimit);
  56. playerCameraParent.localRotation = Quaternion.Euler(rotation.x, 0, 0);
  57. transform.eulerAngles = new Vector2(0, rotation.y);
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement