- Home /
Find gameobjects in a crosshair?
Hi, I have a gameobject attached 3 units in front of my plane. I convert the wolrd space to a gui rect and then I draw the gui. I'm trying to figure out how to check if there's an enemy within the crosshair rect. I have tried with RaycastHit, but since the gui is not always in the center of the screen, that's very difficult.
What would be the best way to see what gameobject is within the gui rect?
I think you should ray cast from the crosshair ins$$anonymous$$d ,not the center of camera .
That's also what I've done, but I can't find out which angle I should be using since transform.forward (Vector3.forward) isn't the right one :-)
To clarify, the correct enemy target should be visible in the crosshair, from the perspective of the camera? (and not from the plane?)
$$anonymous$$ake sure in the future not to post questions as answers, use comments ins$$anonymous$$d.
When you control the plane, you should position it so that the crosshair is "containing" the enemy (for example, another plane). All I need is just a way to raycast from the crosshair gameobject, so that the raycast goes through the gameobject(enemy) that the crosshair is "containing"
Thanks for the link, it's the closest I have ever gotten yet. But there's still one problem
It does work though, when the crosshair is in the center of the screen.
Thank you SO SO SO much :D I got it working now! :D Thank you Again :D :D :D :D :D :D Feel free to convert to answer :-)
Answer by AlucardJay · Oct 14, 2013 at 12:07 PM
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenPointToRay.html
This returns a ray going from camera through a screen point.
You can use this in a RayCast, check the last example in the RayCast Unity Scripting Reference : http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
Your answer
Follow this Question
Related Questions
Third Person Cross Hair Question 1 Answer
Help with Raycast C# 0 Answers
Using raycast and collider to increase int 1 Answer
NullReference when checking tag via Raycast. How to solve this? 1 Answer
Raycast exit point of collider 0 Answers