How to create an array of touch positions?

Edit: I think people misunderstood the question yesterday, and to be honest my way of putting it forward didn’t help and I thought ArcaneX answered it but turns out it didn’t return what I needed exactly. I need to be able to collect all returned touch positions into an array so, for example, when a user touches the screen or moves his finger a position is returned and I would like to collect these positions (Even if there’s one or multiple fingers on screen)into an array so I can loop through them and compare two separate points. Please feel free to ask me a question if you don’t understand what I’m asking. Thanks for the help so far!

Hi everyone, I need to create an of array touch positions and then compare two points.
To get an idea of what I’m asking here’s what I have been trying(see below).

As you can see I want to be able to compare one touch position with all other returned touch positions for each position returned by the touch input when moved. Can anyone help me implement this please, thanks in advance.

if(Input.touchCount > 0)
{
  if(Input.GetTouch(0).phase == Touchphase.Began)
  {
     if(Input.GetTouch(0).phase == Touchphase.Moved)
     {
        var movePos : Vector2[]; //Create array of Vector2's
        for(var i : int = 0; i < movePos.length; i++)
        {
            movePos _= Input.GetTouch(0).position*;*_

for(var j : int = j+1; j < movePos.length; j++)
{
movePos[j] = Input.GetTouch(0).position[j];
}
}
}
}
}

I couldn't get the code to appear right even though the preview pane showed it correctly so, I've left the question editable. If someone could make the code pane more clear that'd be great!

–

1 Answer

1

Assuming you can multitouch, then to retrieve all the touches use Input.touches. To get a specific touch coordinates, you must use Input.touches_.position. You can’t use position* the way you did it, because position is a Vector2, and not an array. You can use Vector2 indexer to get x/y coordinates, but this is another story…_
To create array of positions you can use:
var movePos : Vector2[] = new Vector2[Input.touches.length]; // please note that this array is initialized - in your example it was not
for(var i : int = 0; i < movePos.length; i++)
_
{_
movePos _= Input.touches.position;
}
EDIT: btw - you can’t do:
if(Input.GetTouch(0).phase == Touchphase.Began)
{
if(Input.GetTouch(0).phase == Touchphase.Moved)
because if phase of first touch is Began, then next line it won’t be Moved.
EDIT2: after exchanging a few comments, it looks like array is not appropriate for what you need, because we don’t know how many elements will be stored in it. Generic list will be best. Sample code for one finger:
import System.Collections.Generic; // this must be at the beginning of your script*

* var movePos : List. = new List.();_
private var lastFingerReleaseTime : float;*
* function Update()
{
if (Input.touchCount > 0)
{
var touch : Touch = Input.GetTouch(0);_

if(touch.phase == TouchPhase.Began && Time.time - lastFingerReleaseTime > 2)*
* {
movePos.Clear();
}
else if(touch.phase == TouchPhase.Ended)
{_

lastFingerReleaseTime = Time.time;*
* }
movePos.Add(touch.position);
}
}*

Unfortunately I’m unable to test this, so there might be some error, but general idea remains._

Brilliant, this gives me exactly what I want thank you very much! And, yeah, I don't know why I left that code in that you said I was in a bit of a rush when creating this example code. Thanks again! Edit Sorry but this isn't exactly what I was looking for but it was really close if you could help again it'd be very appreciated!

–

Btw - editing a question/comment doesn't trigger a mail, so if you want to get some attention, at add a new comment.

–

The answer I submitted: var movePos : Vector2[] = new Vector2[Input.touches.length]; var arrayPos = new Array(); for(var i : int = 0; i < movePos.length; i++) { movePos = Input.touches*.position;* arrayPos.Push(movePos); } var builtinArray : Vector2[] = arrayPos.ToBuiltin(Vector2) as Vector2[]; // This part failed

–

Haha I'm very sure you're right! Yes that's it I'd like to be able trace my fingers position and store these positions in an array so I can access them later!

–

Two questions then: 1. Assume you gather a trace over 2 seconds and then user stops touching the screen and touches it again. Should the array be restarted or continued? 2. Do you want to trace fingers in the separate arrays? What if a user touches with three fingers, then removes one finger and then touches again? The problem is that this finger could be registered as touches[0] in the beginning and as touches[2] later. touches[1] will effectively become touches[0] and touches[2]->touches[1]. So tracing in separate arrays might require some additional work.

–