- Home /
Camera following/looking at aircraft
I have the following code so the camera follows the aircraft.
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, transform.position - transform.forward * 10f + transform.up * 2f, 5f * Time.deltaTime);
Camera.main.transform.rotation = Quaternion.Lerp(Camera.main.transform.rotation, transform.rotation, 5f * Time.deltaTime);
I want the camera to be slightly higher the aircraft so you don't just see the back. That's why there is an offset in the position. This is working fine, except now the camera is looking 'over' the aircraft, in stead of 'at'. How do I make sure the camera looks at the aircraft, but still follows it's rotation? Thanks
Answer by nyonge · Oct 18, 2014 at 05:27 PM
1) Create an empty game object that is a CHILD of your aircraft. Name it CameraTarget.
2) Set it where you want the camera to be (5 units behind, 3 units above the jet or something).
3) Make the camera's position follow THAT gameobject, because since it's locally tied to the jet it'll always be in the right spot.
4) Make the camera still rotate towards your aircraft, the rotation code posted should be fine since it's in worldspace.
5) Profit!
At least, that SHOULD work. Depending on any child/parent hierarchies you might have on your camera and aircraft, you might have to play around with local space vs. world space.
Still doesn't work.. When I add:
Camera.main.transform.LookAt (transform.position);
The camera doesn't rotate with the aircraft anymore when it rolls..
Oh, I see, when it rolls too. Don't use LookAt, LookAt will make the camera face towards the aircraft from the position that it's in. What you want to do is move the camera to the position as described above, but match the aircraft's angle of rotation, like the code in your original post.
Yeah, but when I do that, the camera looks 'over' the aircraft in s$$anonymous$$d of 'at', because the camera is higher than the aircraft..
Ahhhh okay, I gotcha. Okay, create a new public Vector3 called "RotationOffset" and set it to the additional degrees that you'd want your camera to face; for example, if you want it to face ten degrees down, do new Vector3(10f,0f,0f); or something like that. It's public, so you can play around with it.
Now, add that rotation vector to your current rotation target. So ins$$anonymous$$d of just
Camera.main.transform.rotation = Quaternion.Lerp(Camera.main.transform.rotation, transform.rotation, 5f * Time.deltaTime);
Try doing
Quaternion RotationTarget = transform.rotation * Quaternion.Euler(RotationOffset);
Camera.main.transform.rotation = Quaternion.Lerp(Camera.main.transform.rotation, RotationTarget, 5f * Time.deltaTime);
So you create the RotationTarget by adding the transform.rotation and Quaternion from the Euler of RotationOffset. (Euler is how you convert a Vector3 to a Quaternion. Also, you add two Quaternions by multiplying them together. $$anonymous$$ath! $$anonymous$$ore info: http://answers.unity3d.com/questions/130510/add-or-substract-two-quaternionsrotations.html )
Hopefully that works!
Your answer
Follow this Question
Related Questions
Camera not rotate when following player 1 Answer
Having a 3D text appear in the middle of the screen without using GUI (C#) 2 Answers
LookRotation is Returning Weird Values 0 Answers
Camera that sits behind player 2 Answers
My look at script not working? 0 Answers