x


Touches only valid for one update?

If I store a Touch in one update, am I not supposed to get the updated data from it the next update?

Doing something like this:

var myTouch:Touch;

function Update()
{
  if(Input.GetTouch(0).phase == TouchPhase.Began)
    myTouch = Input.GetTouch(0);

  Debug.Log(myTouch.position);
}

I expected the log to be updated with the Touch's position if I dragged around my finger. But instead I only get the initial touch position.

Is there a way to store something like this so I don't need to loop through fingerIds every time?

more ▼

asked Dec 09 '10 at 04:11 PM

Mrten gravatar image

Mrten
98 10 10 16

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

The problem is simply that your if-statement is only checking for TouchPhase.Began. This will only be true the first frame. After that, your variable myTouch is not getting updated.

more ▼

answered Dec 09 '10 at 05:30 PM

Bampf gravatar image

Bampf
5k 8 19 49

So is the only solution to store fingerId of the Touch gotten from the GetTouch within the TouchPhase.Began statement, and then use GetTouch every update and check if the Touch returned has the same fingerId as the stored one?

Dec 09 '10 at 07:15 PM Mrten

That might work, but if you aren't writing multitouch behavior then you probably don't need to bother. Just handle the cases where the phase is Cancelled or Ended. (And, if there is suddenly more than 1 touch then treat that as a cancel as well, or walk the array looking for the finger-id you were working with before.) My game only needs single-touches and I have never had to do anything with finger-ids.

Dec 09 '10 at 11:30 PM Bampf

Yeah, the whole reason I want to store away a Touch is because I need multitouch. The reference manual for Input seemed to suggest that this would be possible, and I felt that would be a much more convenient method than constantly looping through the array. Well, I guess I'm stuck with redundancy!

Dec 10 '10 at 09:56 AM Mrten
(comments are locked)
10|3000 characters needed characters left

TouchPhase.Moved what u need.. it sends new position if u hold and drag on screen.With begin u only get the initial position when u out your finger then for dragging u have to use TouchPhase.Moved also u can get the last position that u finished swiping with TouchPhase.Ended

more ▼

answered Jan 17 '11 at 12:27 PM

levye gravatar image

levye
1

(comments are locked)
10|3000 characters needed characters left

Touch is a struct which is a value type. This means that it copies the values instead of having a reference or "link" to the Touch. So on frame one if you save the Touch to a variable it copies the values at that time and saves them but has no idea what that Touch is up to anytime after that. You have to keep updating it every frame. So save the finger ID of the touch so you can find the input that you want in future frames.

Hope that helps.

more ▼

answered Nov 16 '12 at 06:55 AM

yeagz7 gravatar image

yeagz7
46 4

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x956
x583

asked: Dec 09 '10 at 04:11 PM

Seen: 1396 times

Last Updated: Nov 16 '12 at 06:55 AM