Advertisement
Guest User

Playercontroller

a guest
Aug 16th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerControllerScript : MonoBehaviour {
  6.  
  7. [Header("Main settings")]
  8. public Camera mainCamera;
  9. public float moveSpeed;
  10. public float lookSensitivityX;
  11. public float lookSensitivityY;
  12. public float jumpForce;
  13. public float healthPoints;
  14. public float cameraRotLimit;
  15.  
  16. private bool isGrounded;
  17. private Rigidbody myRigid;
  18. private Vector3 vel = Vector3.zero;
  19. private Vector3 rotationX = Vector3.zero;
  20. private float cameraRotation = 0f;
  21. private float currentCamRotX = 0f;
  22. private bool isPaused;
  23.  
  24.  
  25. private int deathHeight;
  26.  
  27.  
  28. // Use this for initialization
  29. void Start () {
  30. isPaused = true;
  31. myRigid = GetComponent<Rigidbody> ();
  32.  
  33. }
  34.  
  35. // Update is called once per frame
  36. void Update () {
  37.  
  38. PlayerMovement ();
  39.  
  40. if (Input.GetKeyDown (KeyCode.Space)) {
  41. myRigid.AddForce (new Vector3 (0f, jumpForce, 0f));
  42. }
  43. if (Input.GetKeyDown (KeyCode.Escape) && isPaused == false) {
  44. isPaused = true;
  45. } else if (Input.GetKeyDown (KeyCode.Escape) && isPaused == true) {
  46. isPaused = false;
  47. }
  48.  
  49. if (isPaused) {
  50. Cursor.lockState = CursorLockMode.Locked;
  51. Cursor.visible = false;
  52. } else if (!isPaused) {
  53. Cursor.lockState = CursorLockMode.None;
  54. Cursor.visible = true;
  55. }
  56. }
  57.  
  58. void FixedUpdate () {
  59.  
  60. //Performs Movement based on velocity variable
  61. if (vel != Vector3.zero) {
  62. myRigid.MovePosition (myRigid.position + vel * Time.fixedDeltaTime);
  63. }
  64.  
  65.  
  66. //Performs the camera rotation
  67. myRigid.MoveRotation (myRigid.rotation * Quaternion.Euler (rotationX));
  68. if (mainCamera != null) {
  69. currentCamRotX -= cameraRotation;
  70. currentCamRotX = Mathf.Clamp (currentCamRotX, -cameraRotLimit, cameraRotLimit);
  71. mainCamera.transform.localEulerAngles = new Vector3 (currentCamRotX, 0f, 0f);
  72. }
  73.  
  74. }
  75.  
  76. void PlayerMovement () {
  77.  
  78. //Final movement vector
  79. Vector3 finalVelocity = (transform.right * Input.GetAxisRaw ("Horizontal") + transform.forward * Input.GetAxisRaw ("Vertical")).normalized * moveSpeed;
  80.  
  81. //Apply movement
  82. Move(finalVelocity);
  83.  
  84. //Calculate rotation as a 3D vector (turning around the y axis)
  85. Vector3 rotX = new Vector3 (0f, Input.GetAxisRaw("Mouse X"), 0f) * lookSensitivityX;
  86.  
  87. //Apply rotation
  88. Rotate (rotX);
  89.  
  90. //Calculate camera rotation as a 3D vector (turning around the X axis)
  91. float rotY = Input.GetAxisRaw ("Mouse Y") * lookSensitivityY;
  92.  
  93. //Apply camera rotation
  94. RotateCamera (rotY);
  95.  
  96. }
  97.  
  98. public void Move (Vector3 velocity) {
  99. vel = velocity;
  100. }
  101. public void Rotate (Vector3 rotX) {
  102. rotationX = rotX;
  103. }
  104. public void RotateCamera (float camRotY){
  105. cameraRotation = camRotY;
  106. }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement