- Home /
Always Place an object in front of camera
Hi All,
I need to find at run-time at which direction camera is looking at. So I thought to to place an object always in front of camera, irrespective of where the camera is currently looking at. E.g. If camera faces towards +x axis, then object should be placed in front of camera along the x axis.
I tried create a cube, make it child of camera and place it at some distance d in front of camera, but as camera moves, child object doesnt move.
I have searched a lot but could not find anything related. I am not planning to attach any Rigidbody component with the object.
Please someone help me out!
If this is a duplicate question then please point me to the thread but please please dont close this thread as I need to solve it urgently.
Thanks in advance!!!!
The child object will move if its parented to the camera and the camera is moving.
What do you want to do with this direction? Camera.main.transform.forward is the direction that the current main camera is looking in.
Hi whydoidoit, does Camera.main.transform.forward always point to the forward of camera or it points to wherever +ve z axis is? I means, if my camera facing -ve y-axis, would Camera.main.transform.forward points to -ve y-axis or +ve z axis?
Please post comments using the "add new comment" button - on UA Answer means Solution and not Reply. I've converted it for you this time.
It is always the direction of the camera - the camera's local Z axis represented as a world space direction.
Answer by HmoudMO · Apr 11, 2013 at 10:18 AM
You can either add the object as a child of the object, or you can have a script as follows:
void Update()
{
transform = Camera.main.transform;
transform.position.z = Camera.main.transform +5 ;
}
Not sure what you are on about with that script - but it's totally wrong.