PlayerController.cs 3.0 KB

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