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
0
Question by MysticalMrCool · Dec 13, 2020 at 05:01 AM · inputtouchtouch controlstouchphase

How to detect whether a particular touch has ended?

Hey guys, Unity noob here. I'm trying to implement touch input into my mobile game and have been having trouble wrapping my head around some things.

I've currently got a cloud character that has animations that I want to have played depending on the touch phase. To detect whether the cloud has been touched I am using RaycastHit2D and I am wanting the first animation to play when the player first touches the cloud, and the next to play when the player releases their finger. The second animation should play regardless of where the finger is on the screen at the time of release (does not have to release finger from the cloud itself).

So basically what I am trying to do is track if the initial touch that began on the cloud object ends, regardless of where it has moved on the screen since beginning the touch on the cloud. Here is what I have got so far:

     Vector3 touchPosWorld;
 
     public GameObject thePlayer;
 
     GameObject cloudGuy;
 
     Touch currentTouch;
 
     // Update is called once per frame
     void Update()
     {
         if (Input.touchCount > 0)
         {
             touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
 
             Vector2 touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y);
 
             RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Camera.main.transform.forward);
 
             
             if (Input.GetTouch(0).phase == TouchPhase.Began)
             {
 
                 if (hitInformation.collider.name == "CloudGuy")
                 {
                     currentTouch = Input.GetTouch(0);
                     cloudGuy = hitInformation.transform.gameObject;
                     cloudGuy.GetComponent<Animator>().SetBool("isTouching", true);
 
                 }
             }
 
             if (currentTouch.phase == TouchPhase.Ended)
             {
                 cloudGuy.GetComponent<Animator>().SetBool("isTouching", false);
                 thePlayer.SetActive(true);
 
             }
 
         }
 
     }

I have tried assigning the Input.GetTouch(0) that was used when the touch began on the cloud object to a variable which I am referencing in the TouchPhase.Ended if statement, however the if statement never seems to be called and the first animation just keeps playing. I have also tried putting the TouchPhase.Ended if statement outside the Input.touchCount > 0 if statement but it makes no difference.

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 Lenda0210 · Dec 13, 2020 at 09:41 AM 0
Share

I think what you need is keeping track of fingerId instead of currentTouch Touch

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eno-Khaon · Dec 13, 2020 at 08:53 PM

A Touch is a struct.

As a result, it is passed by value, and NOT by reference. Edit: By extension, this also means that it's not being populated by reference.

as @Lenda0210 mentions, the most reliable way to track a single finger's input is by keeping track of the fingerId of the touch, since you'll have to keep using Input.GetTouch() to receive updated information in the first place.

 if(Input.touchCount > 0)
 {
     for(int i = 0; i < Input.touchCount; i++)
     {
         Touch currentTouch = Input.GetTouch(i);
         if(currentTouch.fingerId == trackedFingerId && currentTouch.phase == TouchPhase.Ended)
         {
             // Do something
         }
     }
 }
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 MysticalMrCool · Dec 14, 2020 at 09:25 PM 0
Share

Awesome, that's exactly what I was looking for, thank you!

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

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

Get object hovered over by touch 1 Answer

Touch inputs Angry-bird style 0 Answers

check touch position 2 Answers

Reset touch Vector after movement 2 Answers

Check whether touch is held 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