Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by maynull · Mar 06, 2013 at 10:38 PM · iosinputtouchinputmanager

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maynull · Mar 07, 2013 at 12:26 AM 0
Share

No help then

5 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IgorAherne · Dec 24, 2016 at 09:38 PM 0
Share

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)

avatar image
0

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;
     }
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maynull · Mar 07, 2013 at 12:51 AM 0
Share

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...

avatar image maynull · Mar 07, 2013 at 01:00 AM 0
Share
     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...

avatar image buxton4life · Mar 11, 2013 at 05:13 PM 0
Share

How about

    touch.phase == ended

that would be the last touch? Or are you looking for something different with multiple touches?

avatar image maynull · Mar 11, 2013 at 10:31 PM 0
Share

Nah what I mean by that is Latest touch that has started and continuing... Not the ended touch...

avatar image spinki12 · Mar 13, 2013 at 12:11 PM 0
Share

have you sort it out in the end? I'm having exactly the same problem. working on it for nearly two weeks :(

Show more comments
avatar image
0

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();
         }
 
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tony1308 · Dec 01, 2017 at 10:47 PM 0
Share

Ty, this works. I just added this:

Touch lastTouch; private int LastFingerIndex;

avatar image charlesw0817 · May 16, 2019 at 10:53 AM 0
Share

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

avatar image
0

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;
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detect a tap versus standing touch on iPhone? 3 Answers

Touch input reporting 0 delta for first several Updates on iOS, 2 Answers

Best way to get to get touch inputs? 1 Answer

How to fix this problem? ios touch screen? 1 Answer

Touch joystick tutorials ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges