- Home /
Hold down button for in trigger
Hello everyone. I am trying to make a part in my game where you would walk up to a water hole and hold down "e" for 5 seconds and the water integer increases. I have been using a trigger to do this and it has worked so far, but i need to know how to hold the button down for 5 seconds, then it increases the variable. Here is my code (fairly simple)
Thank-You
var playerObj : GameObject;
function OnTriggerStay (other : Collider)
{
if(Input.GetKeyDown("e"))
{
playerObj.GetComponent(GUISurvival_Icons).currentThirst += 10;
}
}
Comment
Answer by SepM · Jul 07, 2014 at 02:43 PM
For the time, you can use delta time. Each intiger of 1 is 1 second in length. You can set an alarm when the trigger is entered and reset it to five when it's left. Here's how I use mine.
public var Alarm1 : float = -0.1;
function Update () {
// Alarm1 activation. While the alarm is greater than zero, raise the water.
if(Alarm1 > 0){
Alarm1 -= Time.deltaTime;
}
// If alarm1 runs out, personally, I prefer set it to -0.1 to keep it below zero. It should've be 5 seconds by here.
if(Alarm1 < 0){
Alarm1 = -0.1;
//Water code.
}
}
If the button is released, just reset the alarm to 5.
I hope this helps out and good luck with your project!
Your answer
![](https://koobas.hobune.stream/wayback/20220613153318im_/https://answers.unity.com/themes/thub/images/avi.jpg)