- Home /
How do I center a child object of camera on other object's screen position?
I have a moveable main camera, and then a child object of that camera that I want to move on its xy plane relative to the camera in a way it stays centered (from the camera's pov) on a moving object in front of it.
So this is the first thing my undercaffeinated brain coughed up :
( this is in Update of the script attached to the tracking object )
myTransform.localPosition = camTransform.InverseTransformPoint(Camera.main.WorldToViewportPoint(Camera.main.ViewportToWorldPoint(headTransform.position)));
And it doesn't work at all, it just sticks the tracking object to the moving object in world space. Before I drive myself crazy with this, does anyone know the proper way to do it ?
Answer by Kiloblargh · May 05, 2012 at 11:00 PM
I figured it out. Answering my own question in case anyone else needs to know:
 var myTransform : Transform;
 var headTransform : Transform;
 var camTransform : Transform;
 var dist : float = 13;
 private var screenPt : Vector3;
 private var worldPt : Vector3;
 function Update () 
 {
 screenPt = Camera.main.WorldToScreenPoint(headTransform.position);
 worldPt = Camera.main.ScreenToWorldPoint(Vector3(screenPt.x,screenPt.y,dist));
 myTransform.localPosition = camTransform.InverseTransformPoint(worldPt);
 }
Answer by Piflik · May 05, 2012 at 05:16 PM
I would shoot a ray from the camera to the center of the screen and place the object on the first point of collision.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                