PlayerController.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using UnityEngine;
  3. public class PlayerController : MonoBehaviour
  4. {
  5. public Rigidbody rb;
  6. public float walkSpeed = 5f;
  7. public float runSpeed = 10f;
  8. public float rotateSpeed = 100f;
  9. public float jumpForce = 5f;
  10. public float stamina = 100f;
  11. public float drainRate = 20f;
  12. public bool bIsSprinting;
  13. public bool bCanSprint;
  14. private float currentSpeed;
  15. //private bool isGrounded;
  16. private Vector2 input;
  17. void Update()
  18. {
  19. input.x = Input.GetAxis("Horizontal");
  20. input.y = Input.GetAxis("Vertical");
  21. bIsSprinting = Input.GetKey(KeyCode.LeftShift) && input.y > 0 && bCanSprint;
  22. currentSpeed = bIsSprinting ? runSpeed : walkSpeed;
  23. if (bIsSprinting)
  24. {
  25. stamina -= drainRate * Time.deltaTime;
  26. }
  27. }
  28. private void FixedUpdate()
  29. {
  30. Vector3 moveDirection = transform.forward * input.y * currentSpeed;
  31. rb.linearVelocity = new Vector3(moveDirection.x, rb.linearVelocity.y, moveDirection.z);
  32. float rotation = input.x * rotateSpeed * Time.fixedDeltaTime;
  33. transform.Rotate(0, rotation, 0);
  34. }
  35. }