Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class PlayerMovement : MonoBehaviour
  7. {
  8. // CharacterController characterController;
  9.  
  10. //Something? Useful stuff
  11. public Transform playerCam;
  12. public Transform orientation;
  13.  
  14. private Rigidbody rb;
  15.  
  16.  
  17. // doubles and floats with unity use floats
  18.  
  19. //this is for character movement
  20. public float maxSpeed = 20;
  21. public float moveSpeed = 4500;
  22. public bool grounded; // Boolean or bool is true or false (also 0 and 1)
  23. public float gravity = 20.0f;
  24.  
  25. // for ground stuff...
  26. public LayerMask whatIsGround;
  27. public float maxSlopeAngle = 35f;
  28.  
  29. //This is controlling camera maths
  30. public float sensitivity = 50f;
  31. public float sensMultiplier = 1f;
  32. public float xRotation;
  33.  
  34. //Variables for input
  35. float x, y;
  36. bool sprinting, jumping;
  37. private Vector3 playerScale;
  38.  
  39. private Vector3 moveDirection = Vector3.zero;
  40.  
  41.  
  42. private bool readyToJump = true;
  43. private float jumpCooldown = 0.25f;
  44. public float jumpForce = 550f;
  45.  
  46. // Start is called before the first frame update
  47. void Start()
  48. {
  49. //characterController = GetComponent<CharacterController>();
  50. playerScale = transform.localScale;
  51. Cursor.lockState = CursorLockMode.Locked;
  52. Cursor.visible = false;
  53.  
  54.  
  55. }
  56. void Awake()
  57. {
  58. rb = GetComponent<Rigidbody>();
  59. }
  60.  
  61. // Update is called once per frame
  62. private void Update()
  63. {
  64. MyInput();
  65. Look();
  66. }
  67. private void FixedUpdate()
  68. {
  69. Movement();
  70. }
  71. private void MyInput()
  72. {
  73. x = Input.GetAxisRaw("Horizontal");
  74. y = Input.GetAxisRaw("Vertical");
  75. jumping = Input.GetButton("Jump");
  76.  
  77. }
  78.  
  79. private void Movement()
  80. {
  81. /*
  82. if (characterController.isGrounded)
  83. {
  84. //This applies movement
  85. moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
  86. moveDirection *= moveSpeed;
  87.  
  88. if (Input.GetButton("Jump"))
  89. {
  90. moveDirection.y = 5.0f;
  91. }
  92.  
  93.  
  94. }
  95.  
  96. characterController.Move(moveDirection * Time.deltaTime);
  97.  
  98. */
  99.  
  100.  
  101. //moveDirection.y -= gravity * Time.deltaTime;
  102.  
  103.  
  104. //This the new formula for movement
  105.  
  106. rb.AddForce(Vector3.down * Time.deltaTime * 10);
  107. // This calls method and Finds velocity to where player is looking
  108. Vector2 mag = FindVelRelativeToLook();
  109. float xMag = mag.x, yMag = mag.y;
  110.  
  111. float maxSpeed = this.maxSpeed;
  112.  
  113. //If the speed is larget than maxspeed, it will cancel out the input so you don't go over max speed.
  114.  
  115. if (x > 0 && xMag > maxSpeed) x = 0;
  116.  
  117. if(x < 0 && xMag < -maxSpeed) x = 0;
  118. if(y > 0 && yMag > maxSpeed) y = 0;
  119. if(y < 0 && yMag < -maxSpeed) y = 0;
  120.  
  121. float mutiplier = 1f, mutiplierV = 1f;
  122.  
  123. if (!grounded)
  124. {
  125. mutiplier = 0.5f;
  126. mutiplierV = 0.5f;
  127. }
  128.  
  129.  
  130.  
  131. //Apply forces to move player
  132. rb.AddForce(orientation.transform.forward * y * moveSpeed * Time.deltaTime * mutiplier * mutiplierV);
  133. rb.AddForce(orientation.transform.forward * x * moveSpeed * Time.deltaTime * mutiplier);
  134.  
  135. }
  136.  
  137. public Vector2 FindVelRelativeToLook()
  138. {
  139.  
  140. float lookAngle = orientation.transform.eulerAngles.y;
  141. float moveAngle = Mathf.Atan2(rb.velocity.x, rb.velocity.z) * Mathf.Rad2Deg;
  142.  
  143. float u = Mathf.DeltaAngle(lookAngle, moveAngle);
  144. float v = 90 - u;
  145.  
  146. float magnitue = rb.velocity.magnitude;
  147. float yMag = magnitue * Mathf.Cos(u * Mathf.Deg2Rad);
  148. float xMag = magnitue * Mathf.Cos(v * Mathf.Deg2Rad);
  149.  
  150. //This go back to void Movemement() and return math formulas
  151. return new Vector2(xMag, yMag);
  152.  
  153.  
  154. }
  155.  
  156. //This will control our camera movement
  157.  
  158. private float desiredX;
  159. private void Look()
  160. {
  161. float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
  162. float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
  163.  
  164. //find current look rotation and location
  165. Vector3 rot = playerCam.transform.localRotation.eulerAngles;
  166. desiredX = rot.y + mouseX;
  167.  
  168. // this will lock camera rotation
  169. xRotation -= mouseY;
  170. xRotation = Mathf.Clamp(xRotation, -90f, 90f);
  171.  
  172. playerCam.transform.localRotation = Quaternion.Euler(xRotation, desiredX, 0);
  173. orientation.transform.localRotation = Quaternion.Euler(0, desiredX, 0);
  174. }
  175. private void OnCollisionStay(Collision other)
  176. {
  177. int layer = other.gameObject.layer;
  178. if (whatIsGround != (whatIsGround | (1 << layer))) return;
  179.  
  180. for (int i = 0; i < other.contactCount; i++)
  181. {
  182. Vector3 normal = other.contacts[i].normal;
  183. //THIS IS FLOOR!
  184. if (isFloor(normal))
  185. {
  186.  
  187. grounded = true;
  188. cancellingGrounded = false;
  189. // normalVector = normal;
  190. CancelInvoke(nameof(StopGrounded));
  191.  
  192. }
  193. }
  194. float delay = 3f;
  195. if (!cancellingGrounded)
  196. {
  197. cancellingGrounded = true;
  198. Invoke(nameof(StopGrounded), Time.deltaTime * delay);
  199. }
  200. }
  201. private bool cancellingGrounded;
  202. private bool isFloor(Vector3 v)
  203. {
  204. float angle = Vector3.Angle(Vector3.up, v);
  205. return angle < maxSlopeAngle;
  206. }
  207. private void StopGrounded()
  208. {
  209. grounded = false;
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement