I am making a simple kart racer, and the camera should be 2 units above the player while following. The problem is that when the track is upside down, the camera goes through the ground, since it is 2 units above the kart. How do I make this function locally? In other words, when the kart is upside down, the local positive y direction is downwards (global frame of reference). How do I make this work with the camera? The camera should always be 2 units above the object locally? So in my upside down example, the camera would be 2 units below the player (global frame of reference). It is kind of like having a parent child relationship, and how the child object follows the parent object’s movements locally. However I cannot have that since the camera lerps its rotation.
I actually ended up figuring it out for myself anyway. I just used a simple raycast and shot it upwards (along local y axis) from the player kart. Then I set the Camera position at a distance of 2 units away from the origin of the raycast.
Ray upRay = new Ray(playerTransform.position, playerTransform.up);
Vector3 camPos= upRay.GetPoint(2);
transform.position = camPos;