Prevent camera from flipping when rotating around origo?

Hello Unity Answers!
I have a character who moves on the surface of a globally fixed cube. I want the camera to look straight on the character and follow the characters movements by circling around origo and look at the character. I don’t want it to rotate the camera if the character rotates etc but just following the movements of the character on the cube.
I have implemented this using transform.LookAt(origo) but my problem is that the camera flips when the character has moved so that the camera would have its upward vector downwards in the global coordinates (because of how LookAt is implemented).

I have the following script in my camera:

    void Start () {
            distance_from_origo = transform.position.magnitude;
    void Update(){
    goto_position = (game_script.getPosition()).normalized*distance_from_origo;
            transform.position = Vector3.Lerp(transform.position, goto_position, 3f* Time.deltaTime);
    //The problem lies here!

Here’s a picture to illustrate the problem:

Well, it’s pretty obvious :wink: Do you see your little arrow that points up? That’s what you pass as “world_up”. Since that vector most likely is just Vector3.up (so in worldspace it’s simply up) you get exactly what you described in your lower images.

The up vector just controls the rotation around the look vector. So you define that you want to look at “this” point and the up vector tell the function “in which direction should be up”. If you want the behaviour you’ve described in your upper images you have to pass the “correct” up vector. However from the information provided you can’t determine which is “correct”.

It looks a bit like you want to have an orbit camera around that point. In this case it’s way easier to place an empty gameobject at the point you want to look at, make the camera a child of it, place it locally at (0, 0, -distanceFromOjbect) by setting the localPosition of the camera to this vector and finally just rotate the empty gameobject manually.

If you use LookAt you have to provide an up vectot that represents the up you want. If you can’t do that, don’t use lookat :wink:

I did eventually solve this a few years ago :slight_smile:
My character is walking around a cube and my method was to store the difference vector between the two faces that the character was traveling between and use this as the up vector.

The big advantage of this is that the camera doesn’t move so much and it looks very natural when you move around a lot on one face and then move to the next face. The camera stays put nicely on one face and can quickly move to the next without causing confusion and it was quite easy to achieve.

If you have a sphere or any other shape you can do this as well by introducing a cube. I feel there must be a better solution though but I’ll just leave this here anyways.

Hello, i have exactly the same issues. Anyone who have resolved this ? Thanks