C# code issue working on player movement and stamina system in unity


C# code issue working on player movement and stamina system in unity



I am currently working on building a custom character movement system for my unity project but I seem to be having some problems with my code, no errors appear in the console and what I am trying to do is create a timer for a stamina system that will fatigued the player overtime and also a recharge system before the player can run again.



The main issue is when I print the values of the current stamina multiple timers are being ran, also the values are not going up appropriately as they should
any help would be appreciated



Thankyou!


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class PlayerController : MonoBehaviour {
CharacterController characterControl;
[SerializeField] float walkSpeed = 10f;
[SerializeField] float sprintSpeed = 15f;
[Header("Stamina")]
[SerializeField] float maxStaminaDecreaseTimer = .5f;
[SerializeField] float maxStaminaIncreaseTime = 1.0f;
[SerializeField] float speedChange = 15f;
float maxStamina = 100f;
float currentStamina;
float currentSpeed;
bool isSprinting = false;
float staminaDecrease = 0.5f;
float staminaIncrease = 1.0f;
//Timer System
float currentStaminaDecreaseTimer;
float currentStaminaIncreaseTimer;
// Movement System
void Start() {
characterControl = GetComponent<CharacterController>();
currentStaminaDecreaseTimer = maxStaminaDecreaseTimer;
currentStaminaIncreaseTimer = maxStaminaIncreaseTime;
currentSpeed = walkSpeed;
currentStamina = maxStamina;
}

void Update() {
MovePlayer();
StaminaSystem();
}

void MovePlayer() {
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");

Vector3 moveSide = transform.right * horizontal * currentSpeed;
Vector3 moveForward = transform.forward * vertical * currentSpeed;

characterControl.SimpleMove(moveSide * Time.deltaTime);
characterControl.SimpleMove(moveForward * Time.deltaTime);
}

void StaminaSystem() {
if (CrossPlatformInputManager.GetButton("Sprint")) {
if (!isSprinting && currentStamina > 0) {
currentSpeed = sprintSpeed;
isSprinting = true;
}
} else {
if (isSprinting) {
currentSpeed = walkSpeed;
isSprinting = false;
}
}

if (isSprinting) {

if (currentStaminaDecreaseTimer <= 0) {
currentStamina -= staminaDecrease;
currentStaminaDecreaseTimer = maxStaminaDecreaseTimer;
}

currentStaminaDecreaseTimer -= Time.deltaTime;
} else if (!isSprinting) {

if (currentStaminaIncreaseTimer <= 0) {
currentStamina += staminaIncrease;
currentStaminaIncreaseTimer = maxStaminaIncreaseTime;
}

if (currentStamina > maxStamina) {
currentStamina = maxStamina;
}
print(currentStamina);

currentStaminaIncreaseTimer -= Time.deltaTime;
}
}
}





This works fine when I run it. That is, currentStamina decreases according to the decrease timer and decrease amount when I hold down the "sprint" button, and increases accordingly when I release the "sprint" button. What's the desired behaviour?
– Adam Stox
Jul 2 at 6:52


currentStamina




2 Answers
2



In this snippet:


if (CrossPlatformInputManager.GetButton("Sprint")) {
if (!isSprinting && currentStamina > 0) {
currentSpeed = sprintSpeed;
isSprinting = true;
}
} else {
if (isSprinting) {
currentSpeed = walkSpeed;
isSprinting = false;
}
}



Each time you enter Update() (with the key pressed), the field isSprinting is toggled on/off. If it is true on entry, it always becomes false, then in the following iteration, that it is false, it becomes true again and so on...


Update()


isSprinting


true


false


false


true





I don't think this is correct. isSprinting gets toggled off only when isSprinting == true AND the "Sprint" button is not held down. And only toggled to true when isSprinting == false and the "Sprint" button IS held down. Checking the value of isSprinting here is actually kind of redundant since you only really care about whether the buttons is pressed and currentStamina.
– Adam Stox
Jul 2 at 6:58


isSprinting


isSprinting == true


isSprinting == false


isSprinting


currentStamina



Hey guys thanks for all the responses i really appreciate them, i was so worked up in my code thinking it was the problem i never realized i had two of the same scripts tied to the game object which in return was giving me interesting behavior XD, well i guess it happens to the best of us!






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages