- Home /
Holding a Touch down on the iphone
I have a strange problem where i want to be able to touch the iphone to make an object move by a force but i want to be able to keep me finger down to make it move constantly until i release my finger.
I have played about with the touchphases where i first tried to keep it as touch began, then tried to say that if not equal to ended or/and canceled but both did not work.
Anyone know how to keep a press held on?
Here is the state of my code that it is at now;
for (var i = 0; i < Input.touchCount; ++i) { var touch: Touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began/*&& jumpo.collider.gameObject.tag == "player"*/) { if(touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) { jumpo.rigidbody.AddForce(Vector3.up * 1000); on.emit = true; } }
else if (touch.phase == TouchPhase.Ended) { on.emit = false; jumpo.rigidbody.AddForce(Vector3.up * 400); }
}
the above code is in a function FixedUpdate so the force can actually be a force instead of doing nothing.
should it be var touch : Touch = Input.GetTouch(i); ins$$anonymous$$d? you're grabbing touch 0 every time around the loop...
i did that change and it still does the same as it already is :(
Answer by rejj · Apr 28, 2011 at 02:33 PM
It looks like you're wrapping everything in a check first for TouchPhase.Began, so your force code will only run when the touch is in the Began phase.
/* this will apply the force whenever a touch starts or is held, and remove it whenever any touch ends (not a concern if you don't use multitouch) */
for (var i = 0; i < Input.touchCount; ++i) {
var touch : Touch = Input.GetTouch(i);
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Cancelled) {
/* apply force here */
}
else if (touch.phase == TouchPhase.Ended) {
/* remove force here */
}
}
perhaps? (untested, sorry)
This is part way correct, you got why he isn't getting the result. I see a problem though. Your code will enable the user to add massive amounts of force to the character by using multiple touches since you're iterating through all of the touches in the array.
Answer by equalsequals · Apr 28, 2011 at 02:39 PM
There are a few problems I see here:
First, you are checking if touch.phase isn't Ended or Canceled inside of the conditional statement which checks for Began. This is a logical error because in order to get to your condition about Ended/Canceled, touch.phase would have to be Began and thus can only ever be true when touch is Began.
Since you want the force added whenever the player is touching the screen, this would be the way to do it:
if(Input.touchCount >= 1) { var touch : Touch = Input.touches[0];
if(touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
jumpo.rigidbody.AddForce(Vector3.up * 1000);
on.emit = true;
}
else
{
on.emit = false;
jumpo.rigidbody.AddForce(Vector3.up * 400);
}
}
Additionally, since you are only ever using the one touch, no need to recursively move through the touches, you can just check if there is 1 or more touches in the array, and grab the first one like you were doing. It's a bit more efficient.
Cheers.
==
Your answer
![](https://koobas.hobune.stream/wayback/20220613125928im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Iphone Unity and Best book . 1 Answer
IsPointerOverGameObject doesn't work on TouchPhase.Ended? 1 Answer
Converting Simple Web game to Iphone 2 Answers
iPhone touch raycasting 1 Answer