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?