- Home /
Camera Rotation Question
I have a camera system set up in a way that requires it's axis to change (specifically, the camera's "up" vector points directly away from the scene origin at all times), but the camera needs to be able to "look around" (like a fps) as if the camera's axis was in the default orientation. I've gotten this to work easily by making an empty gameobject that acts as the camera's axis by using this simple line of code on it:
transform.rotation = Quaternion.FromToRotation (Vector3.up, transform.position);
Then I parent it to the camera and it can rotate properly like a fps camera. My question is how can I achieve the same effect without the empty parent object?
Answer by robertbu · Aug 15, 2014 at 06:52 PM
I think you are looking this line on a script attached to the camera:
transform.rotation = Quaternion.FromToRotation(transform.up, transform.position) * transform.rotation;
I just realized that I made an error in the OP, the parent of the camera's up vector points away from the origin. This allows the camera to look around on the x and y axes. The line of code above allows the camera to look around on the x axis (relative to the screen) but not on the y axis which makes sense for the camera's up vector pointing away from the planet. Sorry about that.
I have to admit I'm confused. If you allow the camera to rotate on two axes, then a parent/child relationship seems ideal to solve this problem. If you wanted to do it without the parent child, you would need to keep track (or derive) the relative offset rotation and combine that with the current 'up' rotation.
Your answer
Follow this Question
Related Questions
"Free" rotation about a sphere 0 Answers
Creating a multiple part turret what locks onto certain axis. 4 Answers
How to force camera rotation? 1 Answer
Edited mouselook script rotation clamp not working 0 Answers
how can i substract one axis by 180? 2 Answers