r/UnityHelp • u/Correct-Valuable5822 • Apr 07 '22
PROGRAMMING Working on a FPS Controller and a Stamina Bar
This Script is the Player Controller (This on the Player)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] Transform playerCamera = null;
[SerializeField] public float mouseSensitivity = 3.5f;
[SerializeField] public float walkSpeed = 4f;
[SerializeField] public float runSpeed = 9.0f;
[SerializeField] public float returntowalkSpeed = 4f;
[SerializeField] public float gravity = -13.0f;
[SerializeField] [Range(0.0f, 0.5f)] float moveSmoothTime = 0.3f;
[SerializeField] [Range(0.0f, 0.5f)] float mouseSmoothTime = 0.03f;
[SerializeField] bool lockCursor = true;
[SerializeField] public bool IsWalking;
float cameraPitch = 0.0f;
float velocityY = 0.0f;
CharacterController controller = null;
Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;
Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;
void Start()
{
controller = GetComponent<CharacterController>();
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
void Update()
{
UpdateMouseLook();
UpdateMovement();
}
void UpdateMouseLook()
{
Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);
cameraPitch -= currentMouseDelta.y * mouseSensitivity;
cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
playerCamera.localEulerAngles = Vector3.right * cameraPitch;
transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
}
void UpdateMovement()
{
Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
targetDir.Normalize();
currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);
if (controller.isGrounded)
velocityY = 0.0f;
velocityY += gravity * Time.deltaTime;
Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + Vector3.up * velocityY;
controller.Move(velocity * Time.deltaTime);
{
//...
if (Input.GetKeyDown(KeyCode.LeftShift))
{
// Set current speed to run if shift is down
walkSpeed = runSpeed;
IsWalking = false;
}
else if (Input.GetKeyUp(KeyCode.LeftShift))
{
// Otherwise set current speed to walking speed
walkSpeed = returntowalkSpeed;
IsWalking = true;
}
}
}
}
This Script is for Stamina (This on a UI Slider)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Stamina : MonoBehaviour
{
PlayerController playerController;
[SerializeField] GameObject player;
public Slider staminaBar;
private int maxStamina = 100;
private float currentStamina;
private float maxValue = 100;
private WaitForSeconds regenTick = new WaitForSeconds(0.1f);
private Coroutine regen;
public static Stamina instance;
//This Awake stuff will deal with Stamina
void Awake()
{
playerController = player.GetComponent<PlayerController>();
instance = this;
}
// Start is called before the first frame update
void Start()
{
currentStamina = maxStamina;
instance.maxValue = maxStamina;
staminaBar.value = maxStamina;
}
// Update is called once per frame
void Update()
{
if(currentStamina > 10)
{
if (Input.GetKey(KeyCode.LeftShift))
{
UseStamina(0.30f);
}
playerController.runSpeed = 9f;
playerController.walkSpeed = 4f;
playerController.returntowalkSpeed = 4f;
}
else if (currentStamina <= 20)
{
playerController.IsWalking = true;
staminaBar.value = currentStamina;
playerController.runSpeed = 4f;
playerController.walkSpeed = 4f;
playerController.returntowalkSpeed = 4f;
}
}
public void UseStamina(float amount)
{
if(currentStamina - amount >= 0)
{
currentStamina -= amount;
staminaBar.value = currentStamina;
if (regen != null)
{
StopCoroutine(regen);
}
regen = StartCoroutine("RegenStamina");
}
}
private IEnumerator RegenStamina()
{
yield return new WaitForSeconds(2);
while (currentStamina < maxStamina)
{
currentStamina += maxStamina / 100;
staminaBar.value = currentStamina;
yield return regenTick;
}
}
}Ok so here is my issue I have made these two scripts in order to make a FPS Game with a Stamina mechanic and everything is working out well except for the fact that my player doesn't start running when I press LShift. However there isn't any errors according to the Unity Editor and it goes away when the Stamina script gets turned off.
I will like some help in trying to figure out why the Player isn't speeding up?
3
Upvotes