Advertisement
Guest User

Untitled

a guest
May 14th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerMovement : MonoBehaviour
  5. {
  6. //camera variables
  7. Vector2 _mouseAbsolute;
  8. Vector2 _smoothMouse;
  9.  
  10. public Vector2 clampInDegrees = new Vector2(360, 180);
  11. public bool lockCursor;
  12. public Vector2 sensitivity = new Vector2(2, 2);
  13. public Vector2 smoothing = new Vector2(3, 3);
  14. public Vector2 targetDirection;
  15. public Vector2 targetCharacterDirection;
  16.  
  17. public GameObject characterBody;
  18.  
  19. //movement variables
  20.  
  21. public float speedright;
  22. public float speedleft;
  23. public float speedforwards;
  24. public float speedback;
  25. public float heightspeed;
  26. public float timesincelastjump;
  27. public float jumpdelay;
  28.  
  29. public float speedwhilesprintright;
  30. public float speedwhilesprintleft;
  31. public float speedwhilesprintforwards;
  32. public float speedwhilesprintback;
  33.  
  34.  
  35. public GameObject Player;
  36. public GameObject Camera;
  37.  
  38. public Rigidbody PlayerRigid;
  39.  
  40. private bool Sprinting;
  41. private bool jumping;
  42.  
  43.  
  44. void Start()
  45. {
  46. PlayerRigid = GetComponent<Rigidbody>();
  47. // Set target direction to the camera's initial orientation.
  48. targetDirection = transform.localRotation.eulerAngles;
  49.  
  50. // Set target direction for the character body to its inital state.
  51. if (characterBody) targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
  52. }
  53.  
  54. void Update ()
  55. {
  56. timesincelastjump += Time.deltaTime;
  57.  
  58. if (Input.GetKey(KeyCode.W))
  59. {
  60. PlayerRigid.AddForce(transform.forward * speedforwards * Time.deltaTime);
  61. }
  62. if (Input.GetKey(KeyCode.A))
  63. {
  64. PlayerRigid.AddForce((transform.right * -1) * speedright * Time.deltaTime);
  65. }
  66. if (Input.GetKey(KeyCode.D))
  67. {
  68. PlayerRigid.AddForce(transform.right * speedright * Time.deltaTime);
  69. }
  70. if (Input.GetKey(KeyCode.S))
  71. {
  72. PlayerRigid.AddForce((transform.forward * -1) * speedright * Time.deltaTime);
  73. }
  74. if (Input.GetKeyDown(KeyCode.Space) && timesincelastjump >= jumpdelay)
  75. {
  76. PlayerRigid.AddForce(transform.up * heightspeed * Time.deltaTime);
  77. timesincelastjump = 0;
  78. }
  79. if (Input.GetKey(KeyCode.LeftShift))
  80. {
  81. Sprinting = true;
  82. speedright = speedwhilesprintright;
  83. speedforwards = speedwhilesprintforwards;
  84. speedleft = speedwhilesprintleft;
  85. speedback = speedwhilesprintback;
  86. }
  87.  
  88. if (!Input.GetKey(KeyCode.LeftShift))
  89. //change this if you want to change base speeds
  90. {
  91. Sprinting = false;
  92. speedright = 4;
  93. speedforwards = 4;
  94. speedleft = 4;
  95. speedback = 4;
  96. }
  97.  
  98. if(lockCursor)
  99. {
  100. if(Input.GetKeyDown(KeyCode.Escape))
  101. {
  102. lockCursor = !lockCursor;
  103. }
  104.  
  105. // Ensure the cursor is always locked when set
  106. Screen.lockCursor = lockCursor;
  107.  
  108. // Allow the script to clamp based on a desired target value.
  109. Quaternion targetOrientation = Quaternion.Euler(targetDirection);
  110. Quaternion targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);
  111.  
  112. // Get raw mouse input for a cleaner reading on more sensitive mice.
  113. Vector2 mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
  114.  
  115. // Scale input against the sensitivity setting and multiply that against the smoothing value.
  116. mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
  117.  
  118. // Interpolate mouse movement over time to apply smoothing delta.
  119. _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
  120. _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
  121.  
  122. // Find the absolute mouse movement value from point zero.
  123. _mouseAbsolute += _smoothMouse;
  124.  
  125. // Clamp and apply the local x value first, so as not to be affected by world transforms.
  126. if (clampInDegrees.x < 360)
  127. _mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);
  128.  
  129. Quaternion xRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right);
  130. transform.localRotation = xRotation;
  131.  
  132. // Then clamp and apply the global y value.
  133. if (clampInDegrees.y < 360)
  134. _mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
  135.  
  136. transform.localRotation *= targetOrientation;
  137.  
  138. // If there's a character body that acts as a parent to the camera
  139. if (characterBody)
  140. {
  141. Quaternion yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, characterBody.transform.up);
  142. characterBody.transform.localRotation = yRotation;
  143. characterBody.transform.localRotation *= targetCharacterOrientation;
  144. }
  145. else
  146. {
  147. Quaternion yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
  148. transform.localRotation *= yRotation;
  149. }
  150. }
  151. else if(Input.GetKeyDown(KeyCode.Escape))
  152. {
  153. lockCursor = !lockCursor;
  154. }
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement