time.deltaTime is not updating to give me an elapsed time
Code below:
The code is trying to capture user inputs for tap, double tap, double tap and hold, hold. To help determine the right input state, I have the standard thresholds for tap inputs, and am trying to capture an elapsed time between when the button is first pressed, and if it is held or pressed a second time. Each time I call time.deltaTime within the loop it returns the same first value (I also tried this with Time.time). I'm not sure if my logic is wrong, if its syntax, if I havent used the loop properly. Hoping for some help as i cant find a solution which fits my needs!
using UnityEngine;
public class PlayerInput : MonoBehaviour { public string verticalAxisName = "Vertical"; //The name of the thruster axis public string horizontalAxisName = "Horizontal"; //The name of the rudder axis
//Values for VehicleMovement.cs
int ButtonCount = 0;
float timePress;
float elapsedTime;
float holdThresh = .8f; // threshold for checking if button is being held
float doubleThresh = .5f; // threshold for checking if button is being pressed twice
float tapWaitTime = 2.0f;
[HideInInspector] public bool tap = false;
[HideInInspector] public bool doubleTap = false;
[HideInInspector] public bool doubleTapBoost = false;
[HideInInspector] public bool hold = false;
[HideInInspector] public float thruster; //The current thruster value
[HideInInspector] public float rudder; //The current rudder value
void Update()
{
//If the player presses the Escape key and this is a build (not the editor), exit the game
if (Input.GetButtonDown("Cancel") && !Application.isEditor)
Application.Quit();
//If a GameManager exists and the game is not active...
if (GameManager.instance != null && !GameManager.instance.IsActiveGame())
{
//...set all inputs to neutral values and exit this method
thruster = rudder = 0f;
return;
}
//Get the values of the thruster, rudder, and brake from the input class
thruster = Input.GetAxis(verticalAxisName);
rudder = Input.GetAxis(horizontalAxisName);
//CAPTURE BUTTON STATE
if (Input.GetButtonDown("Jump"))
{
if (ButtonCount == 0)
{
timePress = Time.deltaTime;
}
ButtonCount += 1;
Debug.Log("ButtonCount 1 = " + ButtonCount);
Debug.Log("Time 0 = " + timePress);
Debug.Log("holdThresh 1 = " + holdThresh);
//////Time.time Test statement
if (ButtonCount == 1)
{
Debug.Log("elapsed time test 1 time.time: " + (Time.deltaTime));
Debug.Log("elapsed time test 1 timePress: " + (timePress));
}
/////////////////////////
//Determine if button is held
if ((Time.deltaTime - timePress) > holdThresh && ButtonCount == 1 && Input.GetButtonUp("Jump"))
{
Debug.Log("Holding");
ButtonCount = 0;
}
//Determine if button is double tapped
else if ((Time.deltaTime - timePress) < doubleThresh && ButtonCount == 2)
{
//First determine if double tap is then held
if ((Time.deltaTime - timePress) > holdThresh && Input.GetButton("Jump"))
{
Debug.Log("Double tapp boost baby!!!!");
ButtonCount = 0;
}
else
{
Debug.Log("Double tapped");
ButtonCount = 0;
}
}
else if ((Time.deltaTime - timePress) < doubleThresh && ButtonCount == 1 && Input.GetButton("Jump"))
{
Debug.Log("Tapped");
Debug.Log("elapsed time test 2 time.time: " + (Time.deltaTime));
Debug.Log("elapsed time test 2 timePress: " + (timePress));
Debug.Log("Button Count 2 = " + ButtonCount);
ButtonCount = 0;
}
}
}
}
i'm pretty sure that GetButtonDown () is only called the frame the button is pushed, if you want it being called every frame the button is held down then you need just GetButton ().
also I your logic is slightly not so logical but i'm busy so i'll get back to you later on that
Answer by NoDumbQuestion · Jul 11, 2018 at 02:01 AM
like @kalen_08 say:
i'm pretty sure that GetButtonDown () is only called the frame the button is pushed, if you want it being called every frame the button is held down then you need just GetButton ().
You call GetButtonDown && GetButtonUp on the same frame. Inside if && if. Your logic is completely wrong and will not work. Change GetButtonDown -> GetButton will not work. You have to rework the entire things.
Wrong:
if (Input.GetButtonDown("Jump"))
{
//Button is press down
if ((Time.deltaTime - timePress) > holdThresh && ButtonCount == 1 && Input.GetButtonUp("Jump"))
{
//I will never be called because button is pressing down and never up
}
}
You could fix this by let if ((Time.deltaTime - timePress) > holdThresh && ButtonCount == 1 && Input.GetButtonUp("Jump"))
out of the If (ButtonDown)