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

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?

You’re on the right track. Break it into steps:

  • Create a MonoBehavior script called, say, BoxHitTester, and attach this to your box. Save and run.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.)
  • 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!

1 Like

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.

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?

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

  • Add an EventSystem to your scene. You might already have one if you have added any UI.
  • Add a PhysicsRaycaster to your main camera.
  • Add a Collider to your desired hit zone. It must be a regular 3D collider. 2D and 3D physics don’t mix.
  • On the same GameObject with the collider, add an EventTrigger component.
  • 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.

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.

1 Like

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.

1 Like

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 :slight_smile: