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?
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!
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.
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.
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