Advertisement
Guest User

Soupertrooper - WoW Controls

a guest
Oct 19th, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.58 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7. //Inputs
  8. public Controls controls;
  9. Vector2 inputNormalized;
  10. [SerializeField] Vector2 inputs;
  11. [SerializeField] float rotation;
  12. [SerializeField] bool run = true;
  13. bool jump;
  14.  
  15. //Velocity
  16. Vector3 velocity;
  17. float gravity = -18f;
  18. float velocityY;
  19. float terminalVelocity = -25f;
  20. [SerializeField] float fallMult;
  21.  
  22. //Running
  23. public float currentSpeed;
  24. public float baseSpeed = 1f;
  25. public float runSpeed = 4;
  26. public float rotateSpeed = 2f;
  27.  
  28. //ground
  29. Vector3 forwardDirection;
  30. Vector3 collisionPoint;
  31. [SerializeField] float slopeAngle;
  32. [SerializeField] float forwardAngle;
  33. [SerializeField] float forwardMult;
  34. Ray groundRay;
  35. RaycastHit groundHit;
  36.  
  37. //Jumping
  38. bool jumping;
  39. float jumpSpeed;
  40. float jumpHeight = 3f;
  41. Vector3 jumpDirection;
  42.  
  43. //DebugGroundNormals
  44. public bool showGroundNormal;
  45. public bool showFallNormal;
  46. public bool showGroundRay;
  47.  
  48. //Component References
  49. CharacterController controller;
  50. public Transform groundDirection;
  51. public Transform fallDirection;
  52.  
  53. // Start is called before the first frame update
  54. void Start()
  55. {
  56. controller = GetComponent<CharacterController>();
  57. }
  58.  
  59. // Update is called once per frame
  60. void Update()
  61. {
  62. GetInputs();
  63. Locomotion();
  64. }
  65.  
  66. void Locomotion()
  67. {
  68. GroundDirection();
  69.  
  70. //Rotating object because cannot rotate controller
  71. Vector3 characterRotation = transform.eulerAngles + new Vector3(0, rotation * rotateSpeed, 0);
  72. transform.eulerAngles = characterRotation;
  73.  
  74. //Running and walking (remove if statement if you want to be able to control direction while falling)
  75. //Slope angle condition to allow for sliding
  76. if(controller.isGrounded && slopeAngle <= controller.slopeLimit)
  77. {
  78. inputNormalized = inputs;
  79. currentSpeed = baseSpeed;
  80. if (run)
  81. {
  82. currentSpeed *= runSpeed;
  83.  
  84. if (inputNormalized.y < 0)
  85. {
  86. currentSpeed = currentSpeed / 2;
  87. }
  88. }
  89. }
  90. else if (!controller.isGrounded || slopeAngle > controller.slopeLimit)
  91. {
  92. inputNormalized = Vector2.Lerp(inputNormalized, Vector2.zero, 0.025f);
  93. currentSpeed = Mathf.Lerp(currentSpeed, 0, 0.025f);
  94. }
  95.  
  96. //Press Space to Jump
  97. if(jump && controller.isGrounded && slopeAngle <= controller.slopeLimit)
  98. {
  99. Jump();
  100. }
  101.  
  102. //Falling, apply gravity if not grounded
  103. if(!controller.isGrounded && velocityY > terminalVelocity)
  104. {
  105. velocityY += gravity * Time.deltaTime;
  106. }
  107. //Lerp speed below is a possible lever for adjusting fall speed on slopes
  108. else if(controller.isGrounded && slopeAngle > controller.slopeLimit)
  109. {
  110. velocityY = Mathf.Lerp(velocityY, terminalVelocity, 0.25f);
  111. }
  112.  
  113. //Applying Inputs (only use top line if you want to control direction during jumps)
  114. if(!jumping)
  115. {
  116. velocity = (groundDirection.forward * inputNormalized.magnitude) * (currentSpeed * forwardMult) + fallDirection.up * (velocityY * fallMult);
  117. }
  118. else
  119. {
  120. velocity = jumpDirection * jumpSpeed + Vector3.up * velocityY;
  121. }
  122.  
  123. //Moving controller
  124. controller.Move(velocity * Time.deltaTime);
  125.  
  126. //Reset Velocity if grounded
  127. if(controller.isGrounded)
  128. {
  129. //Stop Jumping if grounded
  130. if(jumping == true)
  131. {
  132. jumping = false;
  133. }
  134.  
  135. //Stop Gravity if grounded
  136. velocityY = 0;
  137. }
  138.  
  139. }
  140.  
  141. void GroundDirection()
  142. {
  143. //Creating normal from player controller position
  144. //Setting forward direction to controller position
  145. forwardDirection = transform.position;
  146.  
  147. //Creating normal pointing in forward direction
  148. //Setting foward direction based on controller input
  149. if(inputNormalized.magnitude > 0)
  150. {
  151. forwardDirection += transform.forward * inputNormalized.y + transform.right * inputNormalized.x;
  152. }
  153. else
  154. {
  155. forwardDirection += transform.forward;
  156. }
  157.  
  158. //Setting ground direction to look at the forward direction normal
  159. groundDirection.LookAt(forwardDirection);
  160. fallDirection.rotation = transform.rotation;
  161.  
  162. //Setting Ground Ray
  163. groundRay.origin = transform.position + collisionPoint + Vector3.up * 0.05f;
  164. groundRay.direction = Vector3.down;
  165.  
  166. if(showGroundRay)
  167. {
  168. Debug.DrawLine(groundRay.origin, groundRay.origin + Vector3.down * 0.3f, Color.red);
  169. }
  170.  
  171. forwardMult = 1;
  172. fallMult = 1;
  173.  
  174. if(Physics.Raycast(groundRay, out groundHit, 0.3f))
  175. {
  176. //Getting angles
  177. slopeAngle = Vector3.Angle(transform.up, groundHit.normal);
  178. forwardAngle = Vector3.Angle(groundDirection.forward, groundHit.normal) -90;
  179.  
  180. //Slopelimit conditional to allow character to slide downhill
  181. if(forwardAngle < 0 && slopeAngle <= controller.slopeLimit)
  182. {
  183. //Downward slope multiplier to correct for downward speed vector shorting horizontal movement
  184. forwardMult = 1 / Mathf.Cos(forwardAngle * Mathf.Deg2Rad);
  185.  
  186. //Pointing widget down if downhill, ground direction based on forward angle
  187. groundDirection.eulerAngles += new Vector3(-forwardAngle, 0, 0);
  188. }
  189.  
  190. else if (slopeAngle > controller.slopeLimit) //fixing slope slide
  191. {
  192. float groundDistance = Vector3.Distance(groundRay.origin, groundHit.point);
  193. if(groundDistance <= 0.1f)
  194. {
  195. fallMult = 1 / Mathf.Cos((90 - slopeAngle) * Mathf.Deg2Rad);
  196.  
  197. Vector3 groundCross = Vector3.Cross(groundHit.normal, Vector3.up);
  198. fallDirection.rotation = Quaternion.FromToRotation(transform.up, Vector3.Cross(groundCross, groundHit.normal));
  199. }
  200.  
  201. }
  202. }
  203.  
  204.  
  205. DebugGroundNormals();
  206. }
  207.  
  208. void Jump()
  209. {
  210. //Set jumping to true
  211. if(!jumping)
  212. {
  213. jumping = true;
  214. }
  215.  
  216. //Set jumping direction and speed
  217. jumpDirection = (transform.forward * inputs.y + transform.right * inputs.x).normalized;
  218. jumpSpeed = currentSpeed;
  219.  
  220.  
  221. velocityY = Mathf.Sqrt(-gravity * jumpHeight);
  222.  
  223. }
  224.  
  225. void GetInputs()
  226. {
  227. //Y axis controls - Forward/Backwards
  228. //Forwards
  229. if(controls.forwards.GetControlBinding())
  230. {
  231. inputs.y = 1;
  232. }
  233.  
  234. //Backwards
  235. if (controls.backwards.GetControlBinding())
  236. {
  237. if (controls.forwards.GetControlBinding())
  238. {
  239. inputs.y = 0;
  240. }
  241. else
  242. {
  243. inputs.y = -1;
  244. }
  245. }
  246.  
  247. //Forward backward nothing
  248. if (!controls.forwards.GetControlBinding() && !controls.backwards.GetControlBinding())
  249. {
  250. inputs.y = 0;
  251. }
  252.  
  253. //X axis controls - Strafe Left/Right
  254.  
  255. //Strafe Left
  256. if (controls.strafeRight.GetControlBinding())
  257. {
  258. inputs.x = 1;
  259. }
  260.  
  261. //Strafe Right
  262. if (controls.strafeLeft.GetControlBinding())
  263. {
  264. if (controls.strafeRight.GetControlBinding())
  265. {
  266. inputs.x = 0;
  267. }
  268. else
  269. {
  270. inputs.x = -1;
  271. }
  272. }
  273.  
  274. //Strafe Left/Right Nothing
  275. if (!controls.strafeLeft.GetControlBinding() && !controls.strafeRight.GetControlBinding())
  276. {
  277. inputs.x = 0;
  278. }
  279.  
  280. //Rotation Controls Left/Right
  281. if (controls.rotateRight.GetControlBinding())
  282. {
  283. rotation = 1;
  284. }
  285.  
  286. if (controls.rotateLeft.GetControlBinding())
  287. {
  288. if (controls.rotateRight.GetControlBinding())
  289. {
  290. rotation = 0;
  291. }
  292. else
  293. {
  294. rotation = -1;
  295. }
  296. }
  297.  
  298. //Rotation Nothing
  299. if (!controls.rotateLeft.GetControlBinding() && !controls.rotateRight.GetControlBinding())
  300. {
  301. rotation = 0;
  302. }
  303.  
  304. //Toggle Run
  305. if(controls.walkRun.GetControlBindingDown())
  306. {
  307. run = !run;
  308. }
  309.  
  310. //Jumping
  311. jump = controls.jump.GetControlBinding();
  312. }
  313.  
  314. void DebugGroundNormals()
  315. {
  316. Vector3 lineStart = transform.position + Vector3.up * 0.05f;
  317.  
  318. // if(showGroundNormal)
  319. // {
  320. // Debug.DrawLine(lineStart, lineStart + groundDirection.forward * 0.5f, Color.blue);
  321. // }
  322.  
  323. if (showFallNormal)
  324. {
  325. Debug.DrawLine(lineStart, lineStart + fallDirection.up * 0.5f, Color.green);
  326. }
  327. }
  328.  
  329. private void OnControllerColliderHit(ControllerColliderHit hit)
  330. {
  331. collisionPoint = hit.point;
  332. collisionPoint = collisionPoint - transform.position;
  333. }
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement