- Home /
Proper use of Camera WorldToScreenPoint
Hi. So i am trying to move a GUI element on a GUICanvas according to a position of the object in a world.
void UpdateTargetLockPosition(){
targetLockIndicator.transform.position = Camera.main.WorldToScreenPoint(target.transform.position);
}
When i look straight at the object - everything`s fine, but when i look away - it still on the screen, just moving in a reversed directions.
How can i give a restriction to this element movement or drawing? so i wont see it on the screen if i dont see the 3dobject ?
Thanks !
Answer by AlwaysSunny · Jun 02, 2015 at 09:14 PM
Be sure to format your pasted code with the 101010 button.
You could avoid displaying the object if the object is not in front of the camera. Take the dot product of the camera forward and a vector pointing from the camera to the object. If the dot product is less than zero, it's "behind" the camera and you can stop drawing the object.
float dot = Vector3.Dot( (targ.position - cam.position).normalized, cam.forward );
if (dot <= 0) // don't draw ui target marker
Your answer
Follow this Question
Related Questions
How to reposition camera so that given plane point is in given screen position? 1 Answer
Drawing a GUIBox on player/enemies in MiniMap 0 Answers
Raycasting forward from screen perspective instead of game position. 1 Answer
Indicate with markers the direction of Gameobjects which are outside of the camerafield. 0 Answers