- Home /
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[i] = Input.GetTouch(0).position[i];
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!
Answer by ArkaneX · Nov 19, 2013 at 09:05 PM
Assuming you can multitouch, then to retrieve all the touches use Input.touches
. To get a specific touch coordinates, you must use Input.touches[i].position
. You can't use position[i]
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[i] = Input.touches[i].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.<Vector2> = new List.<Vector2>();
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!
I've just read your edit, and according to what you wrote, you need array of positions of each finger touching your screen. So for example, if you touch a screen with one finger, an array with one element is created. If you touch with 4 fingers, then the array should contain 4 elements, etc. $$anonymous$$y code does exactly this.
If you then want to compare points from the array, you can iterate through it using two loops:
for(var i : int = 0; i < movePos.length - 1; i++)
{
for(var j : int = i + 1; j < movePos.length; j++)
{
// compare movePos[i] with movePos[j]
}
}
Above example skips comparing the same elements of an array.
If this still differs from what you need, then I'm afraid you have to share a bit more on what you want to achieve.
Btw - editing a question/comment doesn't trigger a mail, so if you want to get some attention, at add a new comment.
yes you're right your code does do that, but it's still not what I need. I'll try another example, when I touch the screen the engine returns a vector value for every update frame, I'll want to collect all those vector values into an array even if my finger is still. may be my finger moves, then the engine will return another few vector values so an example of what my array may look like after a few frame updates ((1,1,0), (1,1,0), (1,1,0), (1,2,0), (1,3,0)) as you can see my finger doesn't move for three frames and then moves one point up in one frame then another point up in the next update. So basically giving me constant track of my finger position. This what I want added to an array.
Sorry for being complicated, I really appreciate you're help. I've actually submitted an answer which I feel is close to solving this (which is still being moderated) but that came up with an error.
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[i] = Input.touches[i].position;
arrayPos.Push(movePos);
}
var builtinArray : Vector2[] = arrayPos.ToBuiltin(Vector2) as Vector2[]; // This part failed
Your answer
![](https://koobas.hobune.stream/wayback/20220613122232im_/https://answers.unity.com/themes/thub/images/avi.jpg)