- Home /
Hold down a touch input
Hi everybody, I'm making a basketball mini game for android
I need that while I'm holding Down the Button(represented with a guiTexture), the variable called "power" increases its value in order to give more power to the ball and when I release the button, it will throw the ball.
I think that is something like Input.GetButton but I don't Know to do it
so this is my code
function Update () {
for (var touch: Touch in Input.touches)
{
if(touch.phase==TouchPhase.Began&&shoot.HitTest(touch.position))
{
power+=2*Time.deltaTime;
if(power>500)
{
power=500;
}
Debug.Log(power);
}
if(touch.phase == TouchPhase.Ended && shoot.HitTest(touch.position))
{
if (waitTilNextFire <= 0)
{
ballContainer =Instantiate(ball,ballSpawn.transform.position, ballSpawn.transform.rotation);
ballContainer.rigidbody.AddForce(0,6,1*power*Time.deltaTime, ForceMode.Impulse);
waitTilNextFire = 1;
}
}
}
waitTilNextFire -= Time.deltaTime * fireSpeed;
}
thanks in advance
Answer by Graham-Dunnett · May 30, 2013 at 09:32 AM
I think you are almost there. When you get a TouchPhase.Began and you've pressed the shoot button record the current time. Then when the touch ends use the difference in time to control the power of the shot. If you want to visually show the power increasing as the touch is held down, use TouchPhase.Stationary and again compute the power as a difference between the time and the time the touch started.
Something else, use Input.GetTouch() instead of Input.touches.
Your answer