- Home /
GUI Follow RaycastHit
Hello,
Is it possible for a 2D GUI "Reticle" to follow a 3D RaycastHit point.
Kind of the same way World Of Tanks achieves there "Where turret is looking a GUI will follow"
Thanks.
EDIT: At the moment i am using a raycast from the gun, A plane that follows the raycast hit, and a billboard script on the plane with my reticle image on it, All works except it dose not keep its size the further away the raycast hits, Thats why i assumed a GUI would have been better as its glued to the screen? Any help would be greatly appreciated, Thanks.
EDIT2: i Attached a GUI Texture to a empty gameobject then made that Empty gameobject follow raycast hit Hoping the GUI Texture would follow on screen, It did but was extremely limited and sensitive and would not show on screen half the time, Maybe i am going the wrong way about doing this? Thanks.
Answer by robertbu · Jan 15, 2014 at 02:24 PM
Unity has five primary coordinate systems:
GUI: Starts in the upper left of the screen and uses screen pixels.
Screen: Starts in the lower left of the screen and uses screen pixels.
Viewport: Starts at (0,0) in the lower left of the screen and goes to (1,1) in the upper right.
World: 3D world where your standard game objects live. Positions are relative to the world origin.
Local: world coordinates relative to the position and rotation of an object rather than the world origin.
Your RaycastHit is in world coordinates. A GUITexture is in Viewport coordinates. A GUI.DrawTexture() is in GUI coordinates. A GUITexture is a good fit for what you are attempting to do. All you need to do is convert the coordinate from World to Viewport. Something like:
transform.position = Camera.main.WorldToViewportPoint(hit.point);
Answer by nesis · Jan 15, 2014 at 02:37 PM
Raycasting is the right way to go. Get hit.point and then use 'Camera.main.WorldToViewportPoint(hit.point)' to get what position your GUITexture should have. You might also want to perform a check that ensures the target the GUITexture is hovering over is still on screen (I do this below with a dot product measuring the direction the gun is facing and the direction from the gun to the reticle). Without this check, if you end up casting a ray backwards from the camera and hit something, Camera.main.WorldToViewportPoint() will possibly give you its position on the viewport (inverted since it's behind).
For example (in C#-ish code):
public Transform gun;
public void Update() {
Ray ray = new Ray(gun.position,gun.forward);//or whatever you want your ray to be
RaycastHit hit;
if (Physics.Raycast(ray,out hit,Mathf.Infinity)) {
Vector3 directionFromGunToHitPoint = (hit.point-gun.position).normalized;
//the dot product of two normalized vectors measures
//the smallest angle between them... for our purposes,
//if the dot product is greater than 0, we'll change
//the reticle's position and draw it; else, we'll not draw it.
float dotProduct = Vector3.Dot(directionFromGunToHitPoint,gun.forward);
if (dotProduct>0) { //we're looking in the same-ish direction as where the raycast hit, yay
Vector3 viewportPoint = Camera.main.WorldToViewportPoint(hit.point);
reticle.position = viewportPoint;
reticle.guiTexture.enabled = true;
}
else { //disable the guiTexture
reticle.guiTexture.enabled = false;
}
}
}
@nesis - As an alternate to the dot product, just check the Viewport coordinate after converting. If the position is on the screen the viewport coordinate will be in the range of (0,0) to (1,1).
Your answer
Follow this Question
Related Questions
How to exclude OnGUI button from RayCastHit? 2 Answers
Fix a GUI's position relative to a 3D object 1 Answer
Raycast for 2d with touch. 0 Answers
2D Arrow pointing at 3D Position 1 Answer
A 2D GUI in mid air, for oculus, tracking an object. 1 Answer