- Home /
Problem with Boolean
Hello, so I am working on my first game. It is a third person not-yet-shooter. The most recent feature I added was a health bar "borrowed" from Brackeys. I also wanted to make a stamina bar, but it is not working. I have a script named "ThirdPersonMovement" and "AdvanceMovement" and "HealthBarScript".
public class HealthBarScript : MonoBehaviour
{
public Slider healthBar;
public Slider staminaBar;
public Gradient gradientHealth;
public Gradient gradientStamina;
public Image fill;
public void SetMaxHealth(int health)
{
healthBar.maxValue = health;
healthBar.value = health;
fill.color = gradientHealth.Evaluate(1f);
}
public void SetHealth(int health)
{
healthBar.value = health;
fill.color = gradientHealth.Evaluate(healthBar.normalizedValue);
}
public void SetMaxStamina(int stamina)
{
staminaBar.maxValue = stamina;
staminaBar.value = stamina;
fill.color = gradientStamina.Evaluate(1f);
}
public void SetStamina(int stamina)
{
staminaBar.value = stamina;
fill.color = gradientStamina.Evaluate(healthBar.normalizedValue);
}
}
public class AdvanceMovement : MonoBehaviour
{
ThirdPersonMovement basicMovementScript;
public float speedBoost = 10f;
public bool isSprinting;
// Start is called before the first frame update
void Start()
{
basicMovementScript = GetComponent<ThirdPersonMovement>();
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.LeftShift) && isSprinting == false)
{
basicMovementScript.speed += speedBoost;
isSprinting = true;
}
else if (Input.GetKeyDown(KeyCode.LeftShift) && isSprinting == true)
{
basicMovementScript.speed -= speedBoost;
isSprinting = false;
}
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour { public AdvanceMovement isSprinting; AdvanceMovement advanceMovementScript;
public HealthBarScript healthBar;
public HealthBarScript staminaBar;
public int currentHealth;
public int maxHealth = 100;
public int currentStamina;
public int maxStamina = 100;
public CharacterController controller;
public Transform cam;
public float speed = 6f;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
void Start()
{
advanceMovementScript = GetComponent<AdvanceMovement>();
Cursor.visible = false;
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
currentStamina = maxStamina;
staminaBar.SetMaxStamina(maxStamina);
}
void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
}
void TakeStamina(int weak)
{
currentStamina -= weak;
staminaBar.SetStamina(currentStamina);
}
void Update()
{
//Damage
if (Input.GetKeyDown(KeyCode.K))
{
TakeDamage(20);
}
//Weak
if (Input.GetButtonDown("Fire3") && isSprinting == false)
{
TakeStamina(5);
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
TakeStamina(5);
}
// Groundcheck
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < -1)
{
velocity.y = -2f;
}
// Mouselook
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
Cursor.lockState = CursorLockMode.Locked;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
// Jumping
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
} in the ThirdPersonMovement in Update() if Fire3(L Shift) & isSprinting is false, it was supposed to TakeStamina(5); Can you help me resolve this?
something was mixed up here, you posted AdvancedMovement
twice.
to already make your code a bit easier to read:
void Update()
{
if(Input.GetKeyDown(KeyCode.LeftShift) )
{
if(isSprinting)
{
basicMovementScript.speed -= speedBoost;
}else{
basicMovementScript.speed += speedBoost;
}
isSprinting = !isSprinting;
}
}
add debug log instructions inside each if body blocks so you can "see" what is happening. and then go from there.
Answer by SellPet · Feb 12 at 07:27 PM
You dont set nothing from AdvencedMovement to SetStamina. so why do you excpect anything? you need to add this in AdvencedMovement:
if(isSprinting){
SetStamina(stamina)
}
calculate how much you want stamina decrease to be and thats it
I used a fifferent method, but it was not visible in the post because of my mistake. In the ThirdPersonMovement, in Update()
if (Input.GetButtonDown("Fire3") && isSprinting == false) { TakeStamina(5); }
void TakeStamina(int weak) { currentStamina -= weak;
staminaBar.SetStamina(currentStamina);
}
but when playtested, it removed stamina when isSprinting is true
Answer by LukasHuhler · Feb 14 at 04:37 PM
Hello, sorry for not responding for a while, had some stuff going on. I just read the replies and am trying to use the new info. Also, sorry for posting AdvanceMovement twice. I'll try to fix it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613052801im_/https://answers.unity.com/themes/thub/images/avi.jpg)