Search Unity

Yet another 'click gameobject' thread - Bear with me!

Discussion in 'Scripting' started by Philip-ACN, Jan 28, 2019.

  1. Philip-ACN

    Philip-ACN

    Joined:
    Oct 25, 2018
    Posts:
    21
    Hello, I'm trying to return the gameObject under the mouse when clicked.
    Clarification, I have a car object, and I'd like to return the specific part of the object the user clicks on. (Wheel, bonnet, roof etc) The code I have here doesn't ever return anything. Occasionally if I click just to the bottom left of the center of the screen it'll return the ground plane object. But if I move the mouse then return it to the same location it'll stop returning the ground plane.

    Before you say 'Physics.Raycast' and ScreenPointToRay' I'm already doing this. Here is my code:
    Code (CSharp):
    1.     [Header("Camera Objects")]
    2.     [Tooltip("This is the gameObject that contains the AR camera.")]
    3.     public GameObject arCameraParent;
    4.     [Tooltip("This is the gameObject that contains the Non AR camera.")]
    5.     public GameObject nonARCameraParent;
    6.     private Camera activeCamera;
    7.  
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         if (arCameraParent.activeSelf) activeCamera = arCameraParent.GetComponentInChildren<Camera>();
    13.         if (nonARCameraParent.activeSelf) activeCamera = nonARCameraParent.GetComponentInChildren<Camera>();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (Input.GetMouseButtonDown(0))
    20.         {
    21.  
    22.             Ray ray = activeCamera.ScreenPointToRay(Input.mousePosition);
    23.             RaycastHit hit;
    24.  
    25.             if (Physics.Raycast(ray, out hit, 1000f))
    26.             {
    27.                 Debug.Log(hit.transform.gameObject.name);
    28.             }
    29.         }
    30.     }
    The car parts don't have colliders or rigid bodies, but even when I add them, I still didn't get the desired result.

    What stupid mistake am I making?
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Philip-ACN

    There are many wrong assumptions, did you actually read the manual?
    https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

    It explains quite clearly, what you should do, what you should have in order to hit objects and so on, and how you can filter the hits.

    You can also raycast 3D objects using event system, which many might consider to be only useful for UI system: https://docs.unity3d.com/Manual/Raycasters.html - With Physics Raycaster, you can raycast / hit test 3d objects too.
     
  3. Philip-ACN

    Philip-ACN

    Joined:
    Oct 25, 2018
    Posts:
    21
    Wow! Yes. I actually read the manual.
    Obviously not.

    Thanks for your "feedback"
     
  4. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    in your Raycast I think it should be (Physics.Raycast(ray, out hit) no distance.

    Does that change anything?
     
  5. Philip-ACN

    Philip-ACN

    Joined:
    Oct 25, 2018
    Posts:
    21
    I've fixed my issue. (The code was correct, funny that.)

    Turns out that another module which decides on whether the AR or Non AR camera is used was being ran after this module chooses which camera to use, so the raycasts were coming from the wrong camera.
    I moved the previous module to the awake method rather than the start method (so that we choose the correct camera earlier) and everything is as it should be.


    This issue is now resolved.
     
  6. Philip-ACN

    Philip-ACN

    Joined:
    Oct 25, 2018
    Posts:
    21
    Thanks, but my problem was in another castle. :)
     
  7. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    Awesome, actually, I had thought it might of been the cameras because of your dodgy hits in the bottom left, but I haven't worked with more then the maincamera myself, wasn't sure.
     
  8. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    151
    Sounds like a hellish issue to me. Glad to hear you found out. Others might learn from this as well.
     
  9. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637

    Wow! Yes. I actually read the manual.


    It explains quite clearly, what you should do

    Obviously not. Thanks for your "feedback"


    @Philip-ACN , nice attitude when asking for free help.
     
  10. Philip-ACN

    Philip-ACN

    Joined:
    Oct 25, 2018
    Posts:
    21
    It didn't occur to me that one call was being made before another. It's one of those issues you'll only fall for once! lol

    Thanks. Yeah, I hope it helps someone else.

    I appreciate all the help I get, but dismissively saying 'have you read the documentation' isn't helping. This issue is resolved now so we no longer need to argue about it. Have a nice day, i'm off for victory food! :)
     
  11. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Philip-ACN

    Well, you stated in your question
    "The car parts don't have colliders or rigid bodies, but even when I add them, I still didn't get the desired result."

    so that told me that you quite didn't get the basics...So I don't think I was "dismissive".

    If you get the fundamentals wrong, there's probably need to check the manual.

    "so we no longer need to argue about it" - Who was arguing?
     
  12. Philip-ACN

    Philip-ACN

    Joined:
    Oct 25, 2018
    Posts:
    21