Stamina bar
Yes, I know that this has been asked many times, I have researched and tried them all - not just copy/paste as that never works, I actually spent a lot of time on each one I tried trying to adapt it. I do not use a character controller, I have a rigid body, so by the time I comment everything out that gives me errors or changing the wording I am back at square 1. I have a script that has a cool down for a run. I can sprint for 7 seconds. Then I sort of fudged the cooldown in the animator by having it go from sprinting to another branch for 5 seconds where the sprint key won't work. after those 5 seconds the transition is back to the regular branch of the animator where I can sprint again. so I guess I am looking for some kind of GUI slider to indicate this 5 seconds. I can then get rid of that branch of the animator and use the stamina code to allow me to sprint again. so here is my simple code for sprinting
using UnityEngine;
using System.Collections;
public class sprintScript : MonoBehaviour {
private Animator anim;
public float coolDown = 7;
public float coolDownTimer;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
if(coolDownTimer > 0)
{
coolDownTimer -= Time.deltaTime;
}
if (coolDownTimer < 0)
{
coolDownTimer = 0;
anim.ResetTrigger ("Sprint");
anim.SetTrigger ("Idle3");
}
if (Input.GetKeyDown(KeyCode.LeftShift) && coolDownTimer == 0)
{
Attack();
coolDownTimer = coolDown;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
anim.SetTrigger ("Idle3");
}
void Attack(){
anim.SetTrigger ("Sprint");
anim.ResetTrigger ("Idle3");
}
}
Your answer
Follow this Question
Related Questions
How to make a stamina bar? 1 Answer
Adding stamina to the first person controller 1 Answer
NullReferenceException: Object reference not set to an instance of an object error 1 Answer
getkey, keyup, keydown 2 Answers
lerp a float value of keyup 2 Answers