- Home /
Button functions change depending on holding down or tapping Button?
Hi, i need help changing a buttons function depending on whether it was being held down or just tapped. i already got a bit of working code but my problem is that both functions keep being triggered if i hold the button down.
void Update ()
{
if(Input.GetAxisRaw ("Vertical") != -1)
{
currentForwardSpeed = Mathf.SmoothDamp (currentForwardSpeed,
Input.GetAxisRaw ("Vertical") * moveSpeed1,
ref forwaedSpeedV, moveSmoothUse); // used for forward movement
walkOrStepTimer = 0.3f;
// used for debug
//WalkTest = false;
//stepTest = false;
}
if(Input.GetButton("Backward"))
{
walkOrStepTimer -= Time.deltaTime;
if (Input.GetButton("Backward") && walkOrStepTimer <= 0)
{
WalkTest = true;
}
}
if(Input.GetButtonUp("Backward") && walkOrStepTimer > 0)
{
stepTest = true;
}
}
basically i want the player to take a big step backwards if the button is tapped and make the player walk backwards if the button is being held down
right now however as i said both functions keep being triggered if i hold the button down
i already looked a bit around but didn't see anything that could help me any ideas?
putting the GetButton("Backward") outside doesn't change the behavior (i just put it there to make sure it worked when i wrote it). And the reason for (!=1) is because i want to reset the walkOrStepTimer when standing still
Answer by PlayWig · Aug 15, 2015 at 02:09 PM
Ok i found a solution that seems to be working.
all i did was add a bool to check if the player is walking and put the step code before the normal move code
if(Input.GetButtonUp("Backward") && walkOrStepTimer > 0 && !hasWalked)
{
playerMoveVector = new Vector3(0,0,-300); // make the player take a big step back
}
if(Input.GetAxisRaw ("Vertical") != -1)
{
// movement stuff
walkOrStepTimer = 0.3f; // reset timer
hasWalked = false; //reset bool AFTER we've checked that the player has walked
}
if(Input.GetButton("Backward"))
{
// start timer, if the button is released before the timer reaches 0 the player takes a step Backwards
//if the button is released after the timer reaches 0 the player starts walking Backwards
walkOrStepTimer -= Time.deltaTime;
if (Input.GetButton("Backward") && walkOrStepTimer <= 0)
{
hasWalked = true;
// make the player walk backwards
}
}
Your answer
Follow this Question
Related Questions
how add multiple function to my button? 1 Answer
Action executed with more than one key? 1 Answer
Multiple functions in a single button not working 1 Answer
How to make a character run by alternately hitting two buttons? 3 Answers
How do I disable one button out of multiple buttons without spamming declaration? 1 Answer