Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 teseer_zaeem · Nov 30, 2017 at 12:04 AM · mouse click

Physics.RaycastAll() Get the object the pointer is on!

Hi... it's a bit complicated question... I've a cube which has multiple objects on and inside it... I'm being notified whenever the pointer is on it to return me the object's name. The problem is: Whenever I put the pointer on an object, it returns me it's name... but if I moved the pointer to another object which is on top of it... it keeps showing me the old object name. P.S: I could exchange the [0] with [i], but this will return me the last object always... which is something I don't wish to happen. I only wants the last object closest to the pointer position.

Here is my script:

     public LayerMask layermask;
     public LayerMask layermask2;
     public Text txt;
     RaycastHit hit;
     public Material[] red_material;
     public Material[] normal_material;
     public GameObject target;
     Stack s = new Stack();
     void Start()
     {
         normal_material = target.GetComponent<Renderer>().materials;
     }
     void Update()
     {
 
         RaycastHit[] hits;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         hits = Physics.RaycastAll(ray, 100.0f, layermask);
         txt.enabled = false;
         if (hits.Length > 0)
         {
             hit=hits[0];
             if (hits[0].collider.gameObject != target)
             {
                 target.GetComponent<Renderer>().materials = normal_material;
             }
         }
         else
         { 
 
             target.GetComponent<Renderer>().materials = normal_material;
         }
         for (int i = 0; i < hits.Length; i++)
         {
            hits[i].collider.gameObject.transform.position.z)
             target = hit.collider.gameObject;
             s.Push(i);
             target.GetComponent<Renderer>().materials = red_material;
 
             txt.enabled = true;
             txt.text = hit.collider.gameObject.name;
             txt.transform.position = Input.mousePosition + new Vector3(0f, -30f, 0f);
         }
     }
 
Comment
Add comment
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

2 Replies

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

Answer by Buckslice · Nov 30, 2017 at 01:09 AM

I think you could just use Physics.Raycast() instead of Physics.RaycastAll()

Comment
Add comment · Show 5 · 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 teseer_zaeem · Nov 30, 2017 at 03:46 PM 0
Share

Tried to... it only returns if the pointer hits a collider or not, it doesn't allow me to return the gameobject.collider name !

avatar image Multirezonator teseer_zaeem · Nov 30, 2017 at 05:48 PM 0
Share

Prints name of the hitted object:

 print(hits[i].transform.name);
avatar image Buckslice teseer_zaeem · Nov 30, 2017 at 08:02 PM 0
Share
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit hit;
 if(Physics.Raycast(ray, out hit)) {
     Debug.Log(hit.collider.name);
 }
avatar image teseer_zaeem · Dec 02, 2017 at 03:20 PM 0
Share

Buckslice

hanks sir, but I'm having a weird problem.... I wrote your exact code, but with a layermask.. it's still returning me a collider though it's on another layer. *P.S: I can't cancel the big collider I'm going through, or make it's layer "IgnoreRaycast"... it's not an option.

avatar image Buckslice teseer_zaeem · Dec 05, 2017 at 08:01 PM 0
Share

For layermask have a public Layer$$anonymous$$ask mask in your class that you assign in the inspector to the layers you want to hit with the raycast. Then call it like this

 Physics.Raycast(ray, out hit, mask.value)

avatar image
0

Answer by Multirezonator · Nov 30, 2017 at 01:10 AM

There clearly is not enough some condition:

          for (int i = 0; i < hits.Length; i++)
          {
             hits[i].collider.gameObject.transform.position.z) // error here
              target = hit.collider.gameObject;
              s.Push(i); // ??
              target.GetComponent<Renderer>().materials = red_material;
  
              txt.enabled = true;
              txt.text = hit.collider.gameObject.name;
              txt.transform.position = Input.mousePosition + new Vector3(0f, -30f, 0f);
          }

And for what you keep the indexes of the [for] iterations, what will you do with this stack afterwards? If you what to save some RaycastHit info for other Updates - stack it instead indexes of hits[] array.

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 teseer_zaeem · Nov 30, 2017 at 03:45 PM 0
Share

I tried to stack them, but stack will simply won't allow me to stack.push a RaycastHit.... how should I do it?

avatar image Multirezonator teseer_zaeem · Nov 30, 2017 at 05:43 PM 0
Share

I think about something like this:

 s.Push(hits[i]); // ins$$anonymous$$d  s.Push(i);

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

73 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

Related Questions

Help doing mouse click changin shader and back to normal to a gameObject ꜛ! 1 Answer

Click listener not working with imported game objects? 2 Answers

How to I get an object to disappear when I use the mouse to click on it? 1 Answer

Restrict mouse click area 3 Answers

How to avoid multiple mouse clicks registering in OnGUI? 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