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?
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?
You could store the last touches after processing it.
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;
}
}
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.
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*.phase == TouchPhase.Began){*
_ LastFingerIndex = Input.touches*.fingerId;_
_ }_
_ if(Input.touches.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();*
* }*
* }*
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*.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*.fingerId == fingId) {*
idx = i;
break;
}
}
return idx;
}
if (Input.touchCount > 0)
{
for (int i = 0; i < Input.touchCount; i++)
{
if (Input.touches*.phase == TouchPhase.Began)*
{
lastTouch = Input.touches*;*
}
}
}
In my case just get the touch began, because touch just has touchphase = touchphase.began 1 frame