| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using System;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public Rigidbody rb;
- public float walkSpeed = 5f;
- public float runSpeed = 10f;
- public float rotateSpeed = 100f;
- public float jumpForce = 50f;
- public float stamina = 100f;
- public float drainRate = 20f;
- public bool bIsSprinting;
- public bool bCanSprint;
- public int health;
- public bool bHasBeenDamaged;
- private float currentSpeed;
- private bool bIsGrounded;
- public Transform groundCheck;
- public float groundDistance = 0.2f;
- public LayerMask groundMask;
- private Vector2 input;
- void Update()
- {
- input.x = Input.GetAxis("Horizontal");
- input.y = Input.GetAxis("Vertical");
- bIsSprinting = Input.GetKey(KeyCode.LeftShift) && input.y > 0 && bCanSprint;
- currentSpeed = bIsSprinting ? runSpeed : walkSpeed;
- if (bIsSprinting)
- {
- stamina -= drainRate * Time.deltaTime;
- }
- stamina = Mathf.Clamp(stamina, 0, 100f);
- bIsGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
- if (Input.GetButtonDown("Jump") && bIsGrounded)
- {
- Jump();
- }
- /*if (Input.GetKeyDown(KeyCode.F5))
- {
- GameManager.gameManagerInstance.SaveData();
- }*/
- }
- private void FixedUpdate()
- {
- Vector3 moveDirection = transform.forward * input.y * currentSpeed;
- rb.linearVelocity = new Vector3(moveDirection.x, rb.linearVelocity.y, moveDirection.z);
- float rotation = input.x * rotateSpeed * Time.fixedDeltaTime;
- transform.Rotate(0, rotation, 0);
- }
- private void Jump()
- {
- rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
- bIsGrounded = false;
- }
- public void ReduceHealth()
- {
- bHasBeenDamaged = true;
- health--;
- if(health <= 0)
- {
- Debug.Log("Player is dead");
- bHasBeenDamaged = false;
- }
- bHasBeenDamaged = false;
- }
- }
|