- Home /
Delay Player From Using Action Button For A Period Of Time
I've got a script written down that works in conjunction with Time.time and a GUI progress bar. I've got it working and I can get the bar to go from 0 to 1 while holding the Action button. My issue is that once I hit the Action button again, regardless of where in the progress bar I'm at, it resets and starts back at 0.
I want to keep the player from doing anything when the Action button is held down for a period of time if the bar has reached 1. I've been told that the reason It restarts is because of how I am storing the time. Here's the code down below.
var barDisplay : float = 0;
var progressBarEmpty : Texture2D;
var progressBarFull : Texture2D;
private var barStarted : boolean = false;
private var barStartTime : float = 0;
private var barEndTime : float = 0;
private var canDrink : boolean = false;
private var drinkTimer : float = 15.0;
private var isDrinking : boolean = false;
function Start()
{
barDisplay = 0;
barStarted = false;
isDrinking = false;
canDrink = true;
}
function OnGUI()
{
// draw the background:
GUI.BeginGroup (new Rect(Screen.width / 2 - 55, 0, 110, 40));
GUI.Box (Rect (0,0, 110, 40),progressBarEmpty);
// draw the filled-in part:
GUI.BeginGroup (new Rect (0, 0, 110 * barDisplay, 40));
GUI.Box (Rect (0,0, 110, 40),progressBarFull);
GUI.EndGroup ();
GUI.EndGroup ();
}
function Update()
{
// the bar display is linked to the current time,
if(Input.GetButton("Action"))
{
if( barStarted == false )
{
barStarted = true;
barDisplay = 0;
barStartTime = Time.time;
}
else
{
var barCurTime : float = Time.time;
barDisplay = (barCurTime - barStartTime) * 0.5;
barEndTime = barCurTime;
if(barDisplay >= 1)
{
//a result will be inserted here
}
}
}
else
{
barStarted = false;
if(barDisplay > 0)
{
barCurTime = Time.time;
barDisplay += (barEndTime - barCurTime) * 0.0002;
if(barDisplay <= 0)
{
//a result will be inserted here
}
}
}
}
I wrote something that's used for something like keeping the player from shooting if a reload animation is playing for a period of time. Only problem is, I'm not even sure if It'll work, or if it's the smartest way to go about it. Here's that code below too.
private var canDrink : boolean = false;
private var drinkTimer : float = 15.0;
private var isDrinking : boolean = false;
function Update()
{
//Used to stop the Player from drinking for a period of time
if (canDrink == false)
{
canDrink = true;
drinkTimer -= Time.deltaTime;
isDrinking = false;
if(drinkTimer >= 0)
isDrinking = true;
canDrink = false;
}
else
{
if(drinkTimer <=0)
{
isDrinking = false;
canDrink = true;
}
}
}
If anyone could be of help it would be greatly appreciated. I've been trying to solve this for a few weeks now and no luck. Maybe there's something right in front of my face that I'm missing?
Your answer
Follow this Question
Related Questions
How can i keep making the SendMessage repeat as long as the boolean is true 1 Answer
Game Over Screen Problem 3 Answers
I am trying to make a stamina bar and it won't drain no matter what i try. 1 Answer
increment in a value with time.deltatime/time.fixeddeltatime 1 Answer
Simple GUI Problem... 3 Answers