Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Help] - Program a camera ray to hit an area and prompt the UI

Discussion in 'Getting Started' started by PeterRedLine, Jan 8, 2019.

  1. PeterRedLine

    PeterRedLine

    Joined:
    Jan 8, 2019
    Posts:
    5
    Hello,

    I have a super basic interaction to create but am struggling to get my head around how to achieve it in Unity.

    I have a Box and it can rotate when you click and move the mouse, I also need to be able to click an area on the Box and then have a dialogue box that tells you, user, they have selected the correct area. Basically like they are searching the box for a defect and have successfully found it.

    From what I have researched so far, I need to attach a camera ray to my camera so that it can hit a (collider?) attached to the box and then from there it can prompt the dialogue box?

    I have tried a few things but am still really confused how to make the camera ray do what I want and then make something appear on my UI to explain what they have found.

    Would anyone be able to help me with this or direct me to appropriate help?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You're on the right track. Break it into steps:
    1. Create a MonoBehavior script called, say, BoxHitTester, and attach this to your box. Save and run.
    2. Now in the Update method of that script, detect when the mouse button goes down (using Input.GetButtonDown), and simply Debug.Logs a message telling you when this happens. Save and run, and verify that your message appears when you click.
    3. Now create a ray from the camera through the mouse position. The documentation on Camera.ScreenPointToRay has a nice example that not only creates the ray, but (with Debug.DrawRay) draws it in the scene view. So do this. Save and run, click, and then immediately pause the game and switch to the scene view (or have your scene view and game view up side-by-side) so you can verify that the ray looks reasonable.
    4. Add a public Collider property to your script, and drag in a collider which you particularly want to detect hits on. (This could be the whole cube, or some smaller collider that you attach under the cube, or whatever.) Debug.Log the value of this property in your script's Start method. Save, run, and test.
    5. Now in Update, when you detect the mouse button down, use Collider.Raycast to test your ray against this collider. Debug.Log the result. Run and test.
    6. Add a public UnityEvent to your script. When you detect that your ray has hit the collider, Invoke this event. (Watch my 15-minute tutorial on this topic.)
    7. Finally, hook up this event to activate whatever UI you want to show.
    Taken step by step, none of the steps are too hard... but if you get stuck anywhere, post back and we'll help!
     
  3. PeterRedLine

    PeterRedLine

    Joined:
    Jan 8, 2019
    Posts:
    5
    Hello, cheers for the help.

    I think I have taken little bits of information people have given me and misunderstood it and just screwed it up >.<

    The problem is, I only need a specific area of the box to prompt the UI. I tried with putting the 2nd collider in this area, but when I move the box to a different layer it breaks the rotate object code. Maybe I a trying something far too beyond my capability at the moment.

    https://imgur.com/a/jg3B67t
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    If you follow the steps I gave above, you don't need to put the box in a different layer.

    Why not try again, step by step?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I would honestly go way simpler and use Unity's built in ray casting components. Custom ray casting is only needed if you are doing something weird.

    Steps
    1. Add an EventSystem to your scene. You might already have one if you have added any UI.
    2. Add a PhysicsRaycaster to your main camera.
    3. Add a Collider to your desired hit zone. It must be a regular 3D collider. 2D and 3D physics don't mix.
    4. On the same GameObject with the collider, add an EventTrigger component.
    5. Add the method you want to call to the OnClicked section of the EventTrigger.
    The huge advantage of this system is that it will naturally just work with all of the other ray casting you do, like UI elements.
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    That's a good point. I always forget about PhysicsRaycaster and EventTrigger. Even though you've pointed this trick out to me before.

    I do think it adds more layers of mystery that may confuse a newbie... but it certainly gets the job done with a minimum of fuss.
     
    Kiwasi likes this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This is true. I'm often torn between offering "layers of mystery" and "learn it yourself". Hopefully between both our posts we have all options covered.
     
    JoeStrout likes this.
  8. PeterRedLine

    PeterRedLine

    Joined:
    Jan 8, 2019
    Posts:
    5
    Sorry for the delayed response, you have both been very helpful. Now I have time I will play around with what both of you have suggested and see if I can make something work.

    I appreciate the time you have both taken to assist me with this :)