- Home /
Way to detect and get last/latest TOUCH
Hello, I'm trying to detect/get the last/latest touch. I don't need anything else just the latest touch.
Are there any easy way to do that?
Answer by amphoterik · Mar 13, 2013 at 02:07 PM
Touch lastTouch;
if(Input.touches.Length > 0)
lastTouch = Input.touches[Input.touches.Length - 1];
In the above code, if the user is touching the screen at all, then the most recent touch is recorded. For instance, if there is 1 touch, then that is last touch. If a second touch occurs, then that is the one used.
Note that I do not have a mobile device set up currently for testing so my syntax might be off. The gist is correct though.
not really, - if previous touches are released while newer ones are held, then further, newer touches will be placed in the previous indices first (and not on the last indices)
Answer by Statement · Mar 07, 2013 at 12:39 AM
You could store the last touches after processing it.
http://docs.unity3d.com/Documentation/ScriptReference/Input-touches.html
public class Example : MonoBehaviour
{
// Keep a copy of the last frames touches.
Touch[] lastTouches = new Touch[] {};
void Update()
{
Touch[] touches = Input.touches;
// ... Process touches, lastTouches ...
lastTouches = touches;
}
}
Hmm to be clear, what I want to do is: User touches the screen with one finger lasttouch = that touch User touches the screen with 2nd finger while first one still on the screen lasttouch = 2nd finger User releases first finger from screen lasttouch = 2nd finger still User touches screen with finger again now lasttouch = current finger
The problem is that, when you put 1st finger then 2nd, when you release 1 finger last index is correct finger, but when user touches again, last index is still the previous finger because I think unity prepends the list because he was touched with his finger before...
void Update()
{
for(int i = 0; i < Input.touches; i++) {
if(Input.touches[i].phase == TouchPhase.Began)
{
lastFingerIndex = Input.touches[i].fingerId;
}
if(Input.touches[i].fingerid == lastFingerIndex)
{ lastIndex = i;
}
}
Touch lastTouch = Input.GetTouch(lastIndex);
}
I tried something like this but the way that it works sucks.. Need a better way...
How about
touch.phase == ended
that would be the last touch? Or are you looking for something different with multiple touches?
Nah what I mean by that is Latest touch that has started and continuing... Not the ended touch...
have you sort it out in the end? I'm having exactly the same problem. working on it for nearly two weeks :(
Answer by Evan-Pierre · Sep 22, 2017 at 08:11 PM
Just saw this post.. might be bit late but this is what i did and it works fine. I'm using last touch to determine my movement
void TouchMovement(){
float ScreenMid = Screen.width / 2;
for(int i = 0; i < Input.touchCount; i++){
if(Input.touches[i].phase == TouchPhase.Began){
LastFingerIndex = Input.touches[i].fingerId;
}
if(Input.touches[i].phase == TouchPhase.Ended){
lastTouch = Input.touches[LastFingerIndex - 1];
}else{
lastTouch = Input.touches[LastFingerIndex];
}
}
if(lastTouch.position.x < ScreenMid){
MoveLeft();
}else{
MoveRight();
}
if(Input.touchCount == 0){
ZeroVelocity();
}
}
Ty, this works. I just added this:
Touch lastTouch; private int LastFingerIndex;
thanks man it really works for me
I use this
if (Input.touchCount > touchCount)
{
for (int i = 0; i < Input.touchCount; i++)
{
if (Input.touches[i].phase == TouchPhase.Began)
{
fingerNum = Input.touches[i].fingerId;
}
}
}
touchCount = Input.touchCount;
then use fingerId to identify touches
Answer by Zmeyk · Nov 06, 2019 at 11:59 AM
just in case )) main thought - Touch ID is not equivalent to Finger ID.
We can find last Touch ID by:
int storedLastTouchId = 0; //value should be stored all time
int LastTouchIdx() {
for (int i = 0; i < Input.touchCount; i++) {
if (Input.touches[i].phase == TouchPhase.Began)
storedLastTouchId = i;
}
return storedLastTouchId;
}
Then get Finger ID
int localLastIdx = LastTouchIdx();
touchFingerID = Input.touches[localLastIdx].fingerId;
And find back Touch ID by Finger ID with
int GetTouchIdByFingerID(int fingId) {
int idx = 0;
for (int i = 0; i < Input.touchCount; i++) {
if (Input.touches[i].fingerId == fingId) {
idx = i;
break;
}
}
return idx;
}
Answer by luuhoang231 · Jun 15, 2020 at 03:52 AM
if (Input.touchCount > 0)
{
for (int i = 0; i < Input.touchCount; i++)
{
if (Input.touches[i].phase == TouchPhase.Began)
{
lastTouch = Input.touches[i];
}
}
}
In my case just get the touch began, because touch just has touchphase = touchphase.began 1 frame