PlayerController.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. /*if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
  28. {
  29. rb.linearVelocity = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z);
  30. rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
  31. isGrounded = false;
  32. }*/
  33. }
  34. private void FixedUpdate()
  35. {
  36. Vector3 moveDirection = transform.forward * input.y * currentSpeed;
  37. rb.linearVelocity = new Vector3(moveDirection.x, rb.linearVelocity.y, moveDirection.z);
  38. float rotation = input.x * rotateSpeed * Time.fixedDeltaTime;
  39. transform.Rotate(0, rotation, 0);
  40. }
  41. /* private void OnCollisionStay(Collision collision)
  42. {
  43. foreach (ContactPoint contact in collision.contacts)
  44. {
  45. if (contact.normal.y > 0.7f)
  46. {
  47. //isGrounded = true;
  48. return;
  49. }
  50. }
  51. }*/
  52. //private void OnCollisionExit(Collision collision) => isGrounded = false;
  53. }