• 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
Question by refsus · Aug 06, 2014 at 07:30 AM · gameobjectcollidertouch

touch detection within area of collider of gameobject

I want to touch a gameobject based on collider, when I touch the collider, the gameobject change their color, and when my finger leave the area of collider but my finger still touch the screen, color of gameobject back to normally, how I can do this?

  foreach (Touch touch in Input.touches)
         {
             if (touch.phase == TouchPhase.Began)
             {
                 Ray ray = Camera.main.ScreenPointToRay(touch.position);
                 RaycastHit hit = new RaycastHit();
                 if (Physics.Raycast(ray, out hit, 1000.0f))
                 {
                     if (hit.collider.gameObject.name == "1")
                     {
                         hit.collider.gameObject.renderer.material.color = Color.red;
                     }
                     if (hit.collider.gameObject.name == "2")
                     {
                         hit.collider.gameObject.renderer.material.color = Color.green;
                     }
                     if (hit.collider.gameObject.name == "3")
                     {
                         hit.collider.gameObject.renderer.material.color = Color.blue;
                     }
                     if (hit.collider.gameObject.name == "4")
                     {
                         hit.collider.gameObject.renderer.material.color = Color.yellow;
                     }
                 }
             }
             if (touch.phase == TouchPhase.Ended)
             {
                 Ray ray = Camera.main.ScreenPointToRay(touch.position);
                 RaycastHit hit = new RaycastHit();
                 if (Physics.Raycast(ray, out hit, 1000.0f))
                 {
                     if (hit.collider.gameObject.name == "1")
                     {
                         hit.collider.gameObject.renderer.material.color = Color.black;
                     }
                     if (hit.collider.gameObject.name == "2")
                     {
                         hit.collider.gameObject.renderer.material.color = Color.black;
                     }
                     if (hit.collider.gameObject.name == "3")
                     {
                         hit.collider.gameObject.renderer.material.color = Color.black;
                     }
                     if (hit.collider.gameObject.name == "4")
                     {
                         hit.collider.gameObject.renderer.material.color = Color.black;
                     }
                 }
             }
         }

but in my script, the gameobject back to the normal color, when I release my finger from screen..

thanks all, sorry for my bad english

Comment
iamagoatm8

People who like this

1 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

· Add your reply
  • Sort: 
avatar image

Answer by robertbu · Aug 06, 2014 at 08:17 AM

There are several approaches to solve this problem. Let me start with a simple one. Put the following code inside a class that will go on each of the objects you want to select. Then, in the inspector, set the over and not over color in on each object in the Inspector.

 public Color enterColor = Color.red;
 public Color exitColor = Color.black;
 
 void OnMouseEnter() {
      renderer.material.color = enterColor;
 }
 
 void OnMouseExit() {
      renderer.material.color = exitColor;
 }

The second approach requires you gather a list of all your game objects that can be selected. An easy way (and it would also simplify your code) is to add a tag to all of your selectable game objects. Say you set the tag to 'Selectable'. Then you could do:

     GameObject[] gos = GameObject.FindGameObjectsWithTag("Selectable");
     foreach (GameObject go in gos) {
         go.renderer.material.color = Color.black;
     }
     foreach (Touch touch in Input.touches)
     {
         if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
         {
             Ray ray = Camera.main.ScreenPointToRay(touch.position);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, 1000.0f))
             {
                 if (hit.collider.gameObject.name == "1")
                 {
                     hit.collider.gameObject.renderer.material.color = Color.red;
                 }
                 if (hit.collider.gameObject.name == "2")
                 {
                     hit.collider.gameObject.renderer.material.color = Color.green;
                 }
                 if (hit.collider.gameObject.name == "3")
                 {
                     hit.collider.gameObject.renderer.material.color = Color.blue;
                 }
                 if (hit.collider.gameObject.name == "4")
                 {
                     hit.collider.gameObject.renderer.material.color = Color.yellow;
                 }
             }
         }

Note this code set all the objects to black every frame, and only those with a finger over get changes back to white. There are more efficient ways with centeral logic, but this a starting point so you can see one way to gather the objects.

And the third way is a mix of the two. The following script (inserted in a class) goes on each selectable game object:

 public Color enterColor = Color.red;
 public Color exitColor = Color.black;
 
 private static Transform selected = null;
 private bool isSelected = false;
 
 void Update() {
     if (isSelected && selected != transform) {
         selected = null;
         isSelected = false;
         renderer.material.color = exitColor;
     }
 }

 public void SelectMe() {
     if (!selected) {
         selected = transform;
         isSelected = true;
         renderer.material.color = enterColor;
     }
 }

Then the Raycast code becomes:

    foreach (Touch touch in Input.touches)
     {
         if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
         {
             Ray ray = Camera.main.ScreenPointToRay(touch.position);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, 1000.0f) && hit.tag == "Selectable")
             {
                  hit.collider.GetComponent<SomeClassName>().SelectMe();
             }
         }
   

This last one uses a static variable and assumes that only 1 object can be selected at a time.

All this code is untested, so you may have an issue or two, but the concepts work.

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 refsus · Aug 06, 2014 at 08:23 AM 0
Share

thanks @robertbu, I will try your code now, I'll tell you the result

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Move 2 gameObject with touch at the same time 0 Answers

Check if collider touching anything 1 Answer

Portion of game object invisible as it passes through collider 0 Answers

Ray cast collider recognition and execution of a script attached to hit object 1 Answer

need help to destroy game object 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