- Home /
How can an UI Element know on which 3D Object it is pointing?
Hi there, How can I realise, for example in a FirstPerson shooter, when an enemy is the area of the crosshair (so that I can play a sound or change the crosshair color...). So that a UI element recognizes what it is showing (from the perspective of the player). I really have no idea at all, I hope that the question is precise enough and I would be very happy to receive an answer! ;-) Thank you in advance, greetings Luca
Answer by BradyIrv · Jun 10, 2019 at 06:16 PM
Assuming that your UI is a camera overlay, you can use a Raycast from the Camera.transform.forward and check for Enemy Layers as collision. Then write a function to change your crosshair color / UI if the raycast is colliding with an enemy. This could prove expensive if you are raycasting every frame so looking into an alternative might be ideal, but its a start. Maybe LineCasting can work too.
Your answer
Follow this Question
Related Questions
Change from 3D to 2D 1 Answer
How can i change this c# script from 3D to 2D ? 0 Answers
C# UI list item drag onto 2d sprite in world space? 1 Answer
How to make a simple line of sight in a 2D top down shooter. 3 Answers
Help check click out of UI 2 Answers