Advertisement
Guest User

Untitled

a guest
Sep 24th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.15 KB | None | 0 0
  1. // PlayerMovement
  2. using System;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7. [Header("Assignables")]
  8. //Assignables
  9. public Transform playerCam;
  10. public Transform orientation;
  11. private Collider playerCollider;
  12. public Rigidbody rb;
  13.  
  14. [Space(10)]
  15.  
  16. public LayerMask whatIsGround;
  17. public LayerMask whatIsWallrunnable;
  18.  
  19. [Header("MovementSettings")]
  20. //Movement Settings
  21. public float sensitivity = 50f;
  22. public float moveSpeed = 4500f;
  23. public float walkSpeed = 20f;
  24. public float runSpeed = 10f;
  25. public bool grounded;
  26. public bool onWall;
  27.  
  28. //Private Floats
  29. private float wallRunGravity = 1f;
  30. private float maxSlopeAngle = 35f;
  31. private float wallRunRotation;
  32. private float slideSlowdown = 0.2f;
  33. private float actualWallRotation;
  34. private float wallRotationVel;
  35. private float desiredX;
  36. private float xRotation;
  37. private float sensMultiplier = 1f;
  38. private float jumpCooldown = 0.25f;
  39. private float jumpForce = 550f;
  40. private float x;
  41. private float y;
  42. private float vel;
  43.  
  44. //Private bools
  45. private bool readyToJump;
  46. private bool jumping;
  47. private bool sprinting;
  48. private bool crouching;
  49. private bool wallRunning;
  50. private bool cancelling;
  51. private bool readyToWallrun = true;
  52. private bool airborne;
  53. private bool onGround;
  54. private bool surfing;
  55. private bool cancellingGrounded;
  56. private bool cancellingWall;
  57. private bool cancellingSurf;
  58.  
  59. //Private Vector3's
  60. private Vector3 grapplePoint;
  61. private Vector3 normalVector;
  62. private Vector3 wallNormalVector;
  63. private Vector3 wallRunPos;
  64. private Vector3 previousLookdir;
  65.  
  66. //Private int
  67. private int nw;
  68.  
  69. //Instance
  70. public static PlayerMovement Instance { get; private set; }
  71.  
  72. private void Awake()
  73. {
  74. Instance = this;
  75. rb = GetComponent<Rigidbody>();
  76.  
  77. if (playerCam == null)
  78. {
  79. playerCam = Camera.main.transform;
  80. }
  81.  
  82. if (orientation == null)
  83. {
  84. orientation = transform;
  85. }
  86. }
  87.  
  88. private void Start()
  89. {
  90. playerCollider = GetComponent<Collider>();
  91. Cursor.lockState = CursorLockMode.Locked;
  92. Cursor.visible = false;
  93. readyToJump = true;
  94. wallNormalVector = Vector3.up;
  95. }
  96.  
  97. private void LateUpdate()
  98. {
  99. //For wallrunning
  100. WallRunning();
  101. }
  102.  
  103. private void FixedUpdate()
  104. {
  105. //For moving
  106. Movement();
  107. }
  108.  
  109. private void Update()
  110. {
  111. //Input
  112. MyInput();
  113. //Looking around
  114. Look();
  115. }
  116.  
  117. //Player input
  118. private void MyInput()
  119. {
  120. x = Input.GetAxisRaw("Horizontal");
  121. y = Input.GetAxisRaw("Vertical");
  122. jumping = Input.GetButton("Jump");
  123. crouching = Input.GetKey(KeyCode.LeftShift);
  124. if (Input.GetKeyDown(KeyCode.LeftShift))
  125. {
  126. StartCrouch();
  127. }
  128. if (Input.GetKeyUp(KeyCode.LeftShift))
  129. {
  130. StopCrouch();
  131. }
  132.  
  133. }
  134.  
  135. //Scale player down
  136. private void StartCrouch()
  137. {
  138. float num = 400f;
  139. base.transform.localScale = new Vector3(1f, 0.5f, 1f);
  140. base.transform.position = new Vector3(base.transform.position.x, base.transform.position.y - 0.5f, base.transform.position.z);
  141. if (rb.velocity.magnitude > 0.1f && grounded)
  142. {
  143. rb.AddForce(orientation.transform.forward * num);
  144. }
  145. }
  146.  
  147. //Scale player to original size
  148. private void StopCrouch()
  149. {
  150. base.transform.localScale = new Vector3(1f, 1.5f, 1f);
  151. base.transform.position = new Vector3(base.transform.position.x, base.transform.position.y + 0.5f, base.transform.position.z);
  152. }
  153.  
  154. //Moving around with WASD
  155. private void Movement()
  156. {
  157. rb.AddForce(Vector3.down * Time.deltaTime * 10f);
  158. Vector2 mag = FindVelRelativeToLook();
  159. float num = mag.x;
  160. float num2 = mag.y;
  161. CounterMovement(x, y, mag);
  162. if (readyToJump && jumping)
  163. {
  164. Jump();
  165. }
  166. float num3 = walkSpeed;
  167. if (sprinting)
  168. {
  169. num3 = runSpeed;
  170. }
  171. if (crouching && grounded && readyToJump)
  172. {
  173. rb.AddForce(Vector3.down * Time.deltaTime * 3000f);
  174. return;
  175. }
  176. if (x > 0f && num > num3)
  177. {
  178. x = 0f;
  179. }
  180. if (x < 0f && num < 0f - num3)
  181. {
  182. x = 0f;
  183. }
  184. if (y > 0f && num2 > num3)
  185. {
  186. y = 0f;
  187. }
  188. if (y < 0f && num2 < 0f - num3)
  189. {
  190. y = 0f;
  191. }
  192. float num4 = 1f;
  193. float num5 = 1f;
  194. if (!grounded)
  195. {
  196. num4 = 0.5f;
  197. num5 = 0.5f;
  198. }
  199. if (grounded && crouching)
  200. {
  201. num5 = 0f;
  202. }
  203. if (wallRunning)
  204. {
  205. num5 = 0.3f;
  206. num4 = 0.3f;
  207. }
  208. if (surfing)
  209. {
  210. num4 = 0.7f;
  211. num5 = 0.3f;
  212. }
  213. rb.AddForce(orientation.transform.forward * y * moveSpeed * Time.deltaTime * num4 * num5);
  214. rb.AddForce(orientation.transform.right * x * moveSpeed * Time.deltaTime * num4);
  215. }
  216.  
  217. //Ready to jump again
  218. private void ResetJump()
  219. {
  220. readyToJump = true;
  221. }
  222.  
  223. //Player go fly
  224. private void Jump()
  225. {
  226. if ((grounded || wallRunning || surfing) && readyToJump)
  227. {
  228. MonoBehaviour.print("jumping");
  229. Vector3 velocity = rb.velocity;
  230. readyToJump = false;
  231. rb.AddForce(Vector2.up * jumpForce * 1.5f);
  232. rb.AddForce(normalVector * jumpForce * 0.5f);
  233. if (rb.velocity.y < 0.5f)
  234. {
  235. rb.velocity = new Vector3(velocity.x, 0f, velocity.z);
  236. }
  237. else if (rb.velocity.y > 0f)
  238. {
  239. rb.velocity = new Vector3(velocity.x, velocity.y / 2f, velocity.z);
  240. }
  241. if (wallRunning)
  242. {
  243. rb.AddForce(wallNormalVector * jumpForce * 3f);
  244. }
  245. Invoke("ResetJump", jumpCooldown);
  246. if (wallRunning)
  247. {
  248. wallRunning = false;
  249. }
  250. }
  251. }
  252.  
  253. //Looking around by using your mouse
  254. private void Look()
  255. {
  256. float num = Input.GetAxis("Mouse X") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
  257. float num2 = Input.GetAxis("Mouse Y") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
  258. desiredX = playerCam.transform.localRotation.eulerAngles.y + num;
  259. xRotation -= num2;
  260. xRotation = Mathf.Clamp(xRotation, -90f, 90f);
  261. FindWallRunRotation();
  262. actualWallRotation = Mathf.SmoothDamp(actualWallRotation, wallRunRotation, ref wallRotationVel, 0.2f);
  263. playerCam.transform.localRotation = Quaternion.Euler(xRotation, desiredX, actualWallRotation);
  264. orientation.transform.localRotation = Quaternion.Euler(0f, desiredX, 0f);
  265. }
  266.  
  267. //Make the player movement feel good
  268. private void CounterMovement(float x, float y, Vector2 mag)
  269. {
  270. if (!grounded || jumping)
  271. {
  272. return;
  273. }
  274. float num = 0.16f;
  275. float num2 = 0.01f;
  276. if (crouching)
  277. {
  278. rb.AddForce(moveSpeed * Time.deltaTime * -rb.velocity.normalized * slideSlowdown);
  279. return;
  280. }
  281. if ((Math.Abs(mag.x) > num2 && Math.Abs(x) < 0.05f) || (mag.x < 0f - num2 && x > 0f) || (mag.x > num2 && x < 0f))
  282. {
  283. rb.AddForce(moveSpeed * orientation.transform.right * Time.deltaTime * (0f - mag.x) * num);
  284. }
  285. if ((Math.Abs(mag.y) > num2 && Math.Abs(y) < 0.05f) || (mag.y < 0f - num2 && y > 0f) || (mag.y > num2 && y < 0f))
  286. {
  287. rb.AddForce(moveSpeed * orientation.transform.forward * Time.deltaTime * (0f - mag.y) * num);
  288. }
  289. if (Mathf.Sqrt(Mathf.Pow(rb.velocity.x, 2f) + Mathf.Pow(rb.velocity.z, 2f)) > walkSpeed)
  290. {
  291. float num3 = rb.velocity.y;
  292. Vector3 vector = rb.velocity.normalized * walkSpeed;
  293. rb.velocity = new Vector3(vector.x, num3, vector.z);
  294. }
  295. }
  296.  
  297. public Vector2 FindVelRelativeToLook()
  298. {
  299. float current = orientation.transform.eulerAngles.y;
  300. float target = Mathf.Atan2(rb.velocity.x, rb.velocity.z) * 57.29578f;
  301. float num = Mathf.DeltaAngle(current, target);
  302. float num2 = 90f - num;
  303. float magnitude = rb.velocity.magnitude;
  304. return new Vector2(y: magnitude * Mathf.Cos(num * ((float)Math.PI / 180f)), x: magnitude * Mathf.Cos(num2 * ((float)Math.PI / 180f)));
  305. }
  306.  
  307. private void FindWallRunRotation()
  308. {
  309. if (!wallRunning)
  310. {
  311. wallRunRotation = 0f;
  312. return;
  313. }
  314. _ = new Vector3(0f, playerCam.transform.rotation.y, 0f).normalized;
  315. new Vector3(0f, 0f, 1f);
  316. float num = 0f;
  317. float current = playerCam.transform.rotation.eulerAngles.y;
  318. if (Math.Abs(wallNormalVector.x - 1f) < 0.1f)
  319. {
  320. num = 90f;
  321. }
  322. else if (Math.Abs(wallNormalVector.x - -1f) < 0.1f)
  323. {
  324. num = 270f;
  325. }
  326. else if (Math.Abs(wallNormalVector.z - 1f) < 0.1f)
  327. {
  328. num = 0f;
  329. }
  330. else if (Math.Abs(wallNormalVector.z - -1f) < 0.1f)
  331. {
  332. num = 180f;
  333. }
  334. num = Vector3.SignedAngle(new Vector3(0f, 0f, 1f), wallNormalVector, Vector3.up);
  335. float num2 = Mathf.DeltaAngle(current, num);
  336. wallRunRotation = (0f - num2 / 90f) * 15f;
  337. if (!readyToWallrun)
  338. {
  339. return;
  340. }
  341. if ((Mathf.Abs(wallRunRotation) < 4f && y > 0f && Math.Abs(x) < 0.1f) || (Mathf.Abs(wallRunRotation) > 22f && y < 0f && Math.Abs(x) < 0.1f))
  342. {
  343. if (!cancelling)
  344. {
  345. cancelling = true;
  346. CancelInvoke("CancelWallrun");
  347. Invoke("CancelWallrun", 0.2f);
  348. }
  349. }
  350. else
  351. {
  352. cancelling = false;
  353. CancelInvoke("CancelWallrun");
  354. }
  355. }
  356.  
  357. private void CancelWallrun()
  358. {
  359. MonoBehaviour.print("cancelled");
  360. Invoke("GetReadyToWallrun", 0.1f);
  361. rb.AddForce(wallNormalVector * 600f);
  362. readyToWallrun = false;
  363. }
  364.  
  365. private void GetReadyToWallrun()
  366. {
  367. readyToWallrun = true;
  368. }
  369.  
  370. private void WallRunning()
  371. {
  372. if (wallRunning)
  373. {
  374. rb.AddForce(-wallNormalVector * Time.deltaTime * moveSpeed);
  375. rb.AddForce(Vector3.up * Time.deltaTime * rb.mass * 100f * wallRunGravity);
  376. }
  377. }
  378.  
  379. private bool IsFloor(Vector3 v)
  380. {
  381. return Vector3.Angle(Vector3.up, v) < maxSlopeAngle;
  382. }
  383.  
  384. private bool IsSurf(Vector3 v)
  385. {
  386. float num = Vector3.Angle(Vector3.up, v);
  387. if (num < 89f)
  388. {
  389. return num > maxSlopeAngle;
  390. }
  391. return false;
  392. }
  393.  
  394. private bool IsWall(Vector3 v)
  395. {
  396. return Math.Abs(90f - Vector3.Angle(Vector3.up, v)) < 0.1f;
  397. }
  398.  
  399. private bool IsRoof(Vector3 v)
  400. {
  401. return v.y == -1f;
  402. }
  403.  
  404. private void StartWallRun(Vector3 normal)
  405. {
  406. if (!grounded && readyToWallrun)
  407. {
  408. wallNormalVector = normal;
  409. float num = 20f;
  410. if (!wallRunning)
  411. {
  412. rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
  413. rb.AddForce(Vector3.up * num, ForceMode.Impulse);
  414. }
  415. wallRunning = true;
  416. }
  417. }
  418.  
  419. private void OnCollisionStay(Collision other)
  420. {
  421. int layer = other.gameObject.layer;
  422. if ((int)whatIsGround != ((int)whatIsGround | (1 << layer)))
  423. {
  424. return;
  425. }
  426. for (int i = 0; i < other.contactCount; i++)
  427. {
  428. Vector3 normal = other.contacts[i].normal;
  429. if (IsFloor(normal))
  430. {
  431. if (wallRunning)
  432. {
  433. wallRunning = false;
  434. }
  435. grounded = true;
  436. normalVector = normal;
  437. cancellingGrounded = false;
  438. CancelInvoke("StopGrounded");
  439. }
  440. if (IsWall(normal) && layer == LayerMask.NameToLayer("Ground"))
  441. {
  442. StartWallRun(normal);
  443. onWall = true;
  444. cancellingWall = false;
  445. CancelInvoke("StopWall");
  446. }
  447. if (IsSurf(normal))
  448. {
  449. surfing = true;
  450. cancellingSurf = false;
  451. CancelInvoke("StopSurf");
  452. }
  453. IsRoof(normal);
  454. }
  455. float num = 3f;
  456. if (!cancellingGrounded)
  457. {
  458. cancellingGrounded = true;
  459. Invoke("StopGrounded", Time.deltaTime * num);
  460. }
  461. if (!cancellingWall)
  462. {
  463. cancellingWall = true;
  464. Invoke("StopWall", Time.deltaTime * num);
  465. }
  466. if (!cancellingSurf)
  467. {
  468. cancellingSurf = true;
  469. Invoke("StopSurf", Time.deltaTime * num);
  470. }
  471. }
  472.  
  473. private void StopGrounded()
  474. {
  475. grounded = false;
  476. }
  477.  
  478. private void StopWall()
  479. {
  480. onWall = false;
  481. wallRunning = false;
  482. }
  483.  
  484. private void StopSurf()
  485. {
  486. surfing = false;
  487. }
  488.  
  489. public Vector3 GetVelocity()
  490. {
  491. return rb.velocity;
  492. }
  493.  
  494. public float GetFallSpeed()
  495. {
  496. return rb.velocity.y;
  497. }
  498.  
  499. public Collider GetPlayerCollider()
  500. {
  501. return playerCollider;
  502. }
  503.  
  504. public Transform GetPlayerCamTransform()
  505. {
  506. return playerCam.transform;
  507. }
  508.  
  509. public bool IsCrouching()
  510. {
  511. return crouching;
  512. }
  513.  
  514. public Rigidbody GetRb()
  515. {
  516. return rb;
  517. }
  518. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement