# 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];
}
}
}
}
}

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;*
* }