| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using UnityEngine;
- using UnityEngine.UI;
- public class PlayerMovement : MonoBehaviour
- {
- [Header("Movement")]
- public float moveSpeed;
- public float groundDrag;
- public float airDrag;
- public float jumpForce;
- public float jumpCooldown;
- public float airMultiplier;
- bool readyToJump;
- public float sprintSpeed = 1.5f;
- public bool isSprinting;
- private float maxStamina = 1;
- private float minStamina = 0;
- public float stamina = 1;
- public Slider staminaBar;
- [Header("Keybinds")]
- public KeyCode jumpKey = KeyCode.Space;
- public KeyCode sprintKey = KeyCode.LeftShift;
- [Header("Ground Check")]
- public float playerHeight;
- public LayerMask whatIsGround;
- bool grounded;
- public Transform orientation;
- float horizontalInput;
- float verticalInput;
- Vector3 moveDirection;
- Rigidbody rb;
- private void Start()
- {
- SnapToGround();
- rb = GetComponent<Rigidbody>();
- rb.freezeRotation = true;
-
- readyToJump = true;
- isSprinting = false;
- }
- void SnapToGround()
- {
- RaycastHit hit;
- if (Physics.Raycast(transform.position, Vector3.down, out hit, 100, whatIsGround))
- {
- Vector3 pos = transform.position;
- pos.y = hit.point.y + 5;
- transform.position = pos;
- }
- }
- private void Update()
- {
- // ground check
- grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.3f, whatIsGround);
- MyInput();
- if (isSprinting && stamina>minStamina)
- {
- stamina -= Time.deltaTime/4;
- }
- else if(!isSprinting && stamina < maxStamina)
- {
- stamina += Time.deltaTime;
- }
- staminaBar.value = stamina;
- // handle drag
- if (grounded)
- rb.linearDamping = groundDrag;
- else
- rb.linearDamping = airDrag;
- }
- private void FixedUpdate()
- {
- MovePlayer();
- }
- private void MyInput()
- {
- horizontalInput = Input.GetAxisRaw("Horizontal");
- verticalInput = Input.GetAxisRaw("Vertical");
- // when to jump
- if(Input.GetKey(jumpKey) && readyToJump && grounded)
- {
- readyToJump = false;
- Jump();
- Invoke(nameof(ResetJump), jumpCooldown);
- }
- if (Input.GetKey(sprintKey))
- {
- isSprinting = true;
- }
- else
- {
- isSprinting = false;
- }
- }
- private void MovePlayer()
- {
- // calculate movement direction
- moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
- if (isSprinting && stamina > 0)
- {
- // on ground
- if (grounded)
- rb.AddForce(moveDirection.normalized * moveSpeed * 10f * sprintSpeed, ForceMode.Force);
- // in air
- else if(!grounded)
- rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier * sprintSpeed, ForceMode.Force);
- }
- else
- {
- // on ground
- if (grounded)
- rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
- // in air
- else if (!grounded)
- rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
- }
-
- }
- private void Jump()
- {
- // reset y velocity
- rb.linearVelocity = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z);
- rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
- }
- private void ResetJump()
- {
- readyToJump = true;
- }
- }
|