Hi, so I want to create a car game, where the camera follows the player gameobject, however when I directly parent it to the player, it also takes into account the x and z axes, meaning if the car spins out of control, the camera will go haywire. Is there a way to make the main camera follow the player while only taking into account rotation of the y axis? Also, the x has a fixed rotation, so if I could set that too, it would be nice
Thanks!
Grab the barf bag!
You could reach for Cinemachine as I imagine it can do this for you.
If you wanna do it yourself, one way is to make a proxy object that only rotates on the axis you want, and then only copy the axis from the player to this proxy object, which the camera is parented below for the right offset.
You can also NOT copy the heading when the car tilts more than a certain amount.
Assuming Y is car up and you want it to pivot around Y,
if (CarTransform.up.y > 0.5f) // car is "fairly" upright (this number is the cosine of the tilt angle when it stops following heading rotation)
{
CameraProxy.rotation = Quaternion.Euler( 0, CarTransform.eulerAngles.y, 0);
}
I think something like that coupled with copying the player position to it every Update would get you pretty close.
2 Likes
Rather than parenting, have the camera set its own position to the player’s position + some offset in LateUpdate().
Alternatively, look into Cinemachine which can do this plus more for you automatically. Cinemachine
2 Likes
Thanks for the suggestions guys, I tried creating a proxy empty, and now it works. Thank you so much!
1 Like