PlayerController.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem.LowLevel;
  5. public class PlayerController : MonoBehaviour
  6. {
  7. public Animator playerAnimator;
  8. public Rigidbody playerRigidbody;
  9. public float walkSpeed, walkBackwardSpeed, oldWalkSpeed, runSpeed, rotateSpeed;
  10. public float jumpHeight = 500f;
  11. public bool isRunning = false;
  12. public bool canRun = true;
  13. public bool isWalking = false;
  14. public bool hasJumped = false;
  15. public bool isGrounded = true;
  16. public Transform playerTransform;
  17. private void FixedUpdate()
  18. {
  19. if (Input.GetKey(KeyCode.W))
  20. {
  21. playerRigidbody.linearVelocity = transform.forward * walkSpeed * Time.deltaTime;
  22. }
  23. if (Input.GetKey(KeyCode.S))
  24. {
  25. playerRigidbody.linearVelocity = -transform.forward * walkBackwardSpeed * Time.deltaTime;
  26. }
  27. }
  28. private void Update()
  29. {
  30. if(Input.GetKeyDown(KeyCode.W))
  31. {
  32. playerAnimator.SetBool("IsWalking", true);
  33. isWalking = true;
  34. }
  35. if (Input.GetKeyUp(KeyCode.W))
  36. {
  37. playerAnimator.SetBool("IsWalking", false);
  38. isWalking = false;
  39. }
  40. if (Input.GetKeyDown(KeyCode.S))
  41. {
  42. playerAnimator.SetBool("IsWalkingBackward", true);
  43. }
  44. if (Input.GetKeyUp(KeyCode.S))
  45. {
  46. playerAnimator.SetBool("IsWalkingBackward", false);
  47. }
  48. if (Input.GetKey(KeyCode.A))
  49. {
  50. playerTransform.Rotate(0, -rotateSpeed * Time.deltaTime, 0);
  51. }
  52. if(Input.GetKey(KeyCode.D))
  53. {
  54. playerTransform.Rotate(0, rotateSpeed * Time.deltaTime, 0);
  55. }
  56. if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
  57. {
  58. playerRigidbody.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
  59. playerAnimator.SetTrigger("Jump");
  60. isGrounded = false;
  61. playerAnimator.SetBool("IsGrounded", false);
  62. }
  63. if (isWalking && canRun)
  64. {
  65. if (Input.GetKeyDown(KeyCode.LeftShift))
  66. {
  67. walkSpeed = walkSpeed + runSpeed;
  68. playerAnimator.SetBool("IsRunning", true);
  69. isRunning = true;
  70. }
  71. if (Input.GetKeyUp(KeyCode.LeftShift))
  72. {
  73. walkSpeed = oldWalkSpeed;
  74. playerAnimator.SetBool("IsRunning", false);
  75. isRunning = false;
  76. }
  77. }
  78. else
  79. {
  80. playerAnimator.SetBool("IsRunning", false);
  81. isRunning = false;
  82. }
  83. }
  84. private void OnCollisionStay(Collision collision)
  85. {
  86. foreach(ContactPoint contact in collision.contacts)
  87. {
  88. if (contact.normal.y > 0.9f)
  89. {
  90. isGrounded = true;
  91. playerAnimator.SetBool("IsGrounded", true);
  92. return;
  93. }
  94. }
  95. }
  96. private void OnCollisionExit(Collision collision)
  97. {
  98. isGrounded = false;
  99. playerAnimator.SetBool("IsGrounded", false);
  100. }
  101. }