Advertisement
FlyingFrog

Untitled

Mar 7th, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. public class FirstPersonMovement : MonoBehaviour
  5. {
  6. public float speed = 5;
  7.  
  8. private float standartMoveSpeed;
  9.  
  10.  
  11.  
  12.  
  13.  
  14. [Header("Running")]
  15. public bool canRun = true;
  16. public bool IsRunning { get; private set; }
  17. public float runSpeed = 9;
  18. public KeyCode runningKey = KeyCode.LeftShift;
  19.  
  20. Rigidbody rigidbody;
  21. /// <summary> Functions to override movement speed. Will use the last added override. </summary>
  22. public List<System.Func<float>> speedOverrides = new List<System.Func<float>>();
  23.  
  24.  
  25.  
  26. void Awake()
  27. {
  28. // Get the rigidbody on this.
  29. rigidbody = GetComponent<Rigidbody>();
  30. }
  31. private void Start () {
  32. standartMoveSpeed = speed;
  33. }
  34. void FixedUpdate()
  35. {
  36. // Update IsRunning from input.
  37. IsRunning = canRun && Input.GetKey(runningKey);
  38.  
  39. // Get targetMovingSpeed.
  40. float targetMovingSpeed = IsRunning ? runSpeed : speed;
  41. if (speedOverrides.Count > 0)
  42. {
  43. targetMovingSpeed = speedOverrides[speedOverrides.Count - 1]();
  44. }
  45.  
  46. // Get targetVelocity from input.
  47. Vector2 targetVelocity =new Vector2( Input.GetAxis("Horizontal") * targetMovingSpeed, Input.GetAxis("Vertical") * targetMovingSpeed);
  48.  
  49. // Apply movement.
  50. rigidbody.velocity = transform.rotation * new Vector3(targetVelocity.x, rigidbody.velocity.y, targetVelocity.y);
  51. }
  52.  
  53. public void IncreasMoveSpeed (float newSpeed){
  54. speed = newSpeed;
  55. }
  56. public void SetStandartSpeed(){
  57. speed =standartMoveSpeed;
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement