• 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 /
This question was closed Jan 07, 2019 at 09:22 AM by Russellcart.
avatar image
Question by Russellcart · Jan 06, 2019 at 11:54 AM · gameobjectmobiletouchtouch controls

Rhythm Game - When I touch an object with mobile in only activates 80% of the time...

Hello, -

I am using android. -

I have a rhythm game where notes fall. This script it attached to the notes so that when you touch them on mobile they disappear. It works well but sometimes it doesn't work.. I touch them but they do not go to the blank sprite and I have no idea why..

I have 2 touches registered below as there is sometimes 2 that spawn at ones so you need to touch them both. Without the second section of code it would only let me press one at once.

 void Update()

{

 foreach (Touch touch in Input.touches)
     {
     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
     {
         touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
         touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y);
         RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Camera.main.transform.forward);

         if (hitInformation.collider.tag != "Button")
         {
             touchedObject = hitInformation.transform.gameObject;
             touchedObject.GetComponent<SpriteRenderer>().sprite = blank;
         }
     }

     if (Input.touchCount > 0 && Input.GetTouch(1).phase == TouchPhase.Began)
     {
         touchPosWorld1 = Camera.main.ScreenToWorldPoint(Input.GetTouch(1).position);
         touchPosWorld2D2 = new Vector2(touchPosWorld1.x, touchPosWorld1.y);
         RaycastHit2D hitInformation2 = Physics2D.Raycast(touchPosWorld2D2, Camera.main.transform.forward);

         if (hitInformation2.collider.tag != "Button")
         {
             touchedObject1 = hitInformation2.transform.gameObject;
             touchedObject1.GetComponent<SpriteRenderer>().sprite = blank;
         }
     }
 }    
 }

I hope you can help.

Thank you very much.

Comment

People who like this

0 Show 0
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

1 Reply

  • Sort: 
avatar image

Answer by RadonRaph · Jan 06, 2019 at 12:43 PM

Hello, You can try this :

  void Update()
         {
             if (Input.touchCount > 0)
             {
                 for (int i = 0; i < Input.touchCount; i++)
                 {
                     Touch touch = Input.GetTouch(i);
                     if (touch.phase == TouchPhase.Began)
                     {
                        Ray ray = Camera.main.ScreenPointToRay(touch.position);
                        RaycastHit2D hitInformation = Physics2D.Raycast(ray.origin, ray.direction);
     
                         if (hitInformation.collider.tag != "Button")
                         {
                             GameObject touchedObject = hitInformation.transform.gameObject;
                             touchedObject.GetComponent<SpriteRenderer>().sprite = blank;
                             Instantiate(partRed, touchedObject.transform.position, Quaternion.identity);
                             partRed.Play();
                         }
                     }
                 }
             }
     
         }

That will test all touches and make a ray directly by the camera.

Raph

Comment

People who like this

0 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 Russellcart · Jan 07, 2019 at 08:14 AM 0
Share

Thank you for your reply and your help. Unfortunately I replaced the script on the 'notes' with your script and still have the problem of some not disappearing when touched.. :(

Do you have any other thoughts?

Thanks again

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

206 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

Related Questions

Mobile Input - Sensitivity, Gravity Matching InputManager's Key 0 Answers

Touch input registered in Unity Remote but not in APK 0 Answers

Possible to get touch area data? 1 Answer

How to make touch controls similar to Bumper.io 0 Answers

How do I move an object with my finger?[C#] 1 Answer


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