Camera follow player around sphere without flipping

Hi,

I have a player character that moves around a sphere, and i want the camera to follow from above, looking down (with a bit of an angle) on the character.

Like this

I had it parented to the player before and that worked perfectly, but i can’t do that anymore for other reasons.
The player doesn’t rotate around itself (local y), it only moves around the planet, meaning that the controls are locked to this view angle.
Also, I can’t use the player rotation, because the player moves on low poly terrain and make sudden rotations, when changing polygon, and that will make the camera move in the same sudden changes.

So far i got the camera to follow the player position around the planet, but i can’t get it to look at the player without flipping at some point.

The position code

        Vector3 v = playerTransform.position - Vector3.zero;
        v += v.normalized * distance;
        transform.position = Vector3.zero + v;

So far i’ve tried LookAt, LookRotation and other things, but they all flip, or turn in some way.


9749833--1395310--upload_2024-4-4_18-44-40.png

Camera view
The controls are moving the player in the directions same as this view. If the camera flips, the controls dont match the direction anymore.
9749833--1395301--upload_2024-4-4_18-39-33.png

9749833--1395301--upload_2024-4-4_18-39-33.png

Smells like euler angles. Don’t rotate eulers, rotate quaternions.

But in any case … did you know Unity has this stuff built-in?
It’s called Cinemachine. You set up the follow target, and at what distance/angle to follow, and you’re set. Not 100% sure about the spherical movement but I doubt this is going to be an issue with Cinemachine.

If you use a sphere collider on your planet then the player will smoothly move around it and then you’ll be able to make the camera a child of your player again.

Hi CodeSmile,

Thanks for the quick answer, but it doesn’t seem to do much difference.
I still had to have the camera follow the object following “orbiting” above the player to get the camera to get the top down view. And then the camera still flips when aiming at the player.

unless i got something wrong here.

What i’m looking for is exactly this. he parents the camera to the player, but i can’t do that because i need to have the player move on a mesh collider, which results in sudden fast camera movement when the player goes from one collider normal to the other.

Hey Zulo3D

dind’t see your post before, but i actually ended up doing something similar. I added a sphere collider and doing a raycast from above the player position, i can get the camera to follow that instead. The player has to move on the mesh collider as there will be bumps and stuff on the terrain.

code in case anyone needs it. this is the camera script in fixed update

        from = player.transform.position * 2;
        direction = player.transform.position - from;
        if (Physics.Raycast(from, direction, out RaycastHit hit, 99999, layerMask))
        {
            transform.position = hit.normal.normalized * distance;

            Quaternion targetRotation = Quaternion.FromToRotation(transform.up, hit.normal.normalized) * transform.rotation;
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 50 * Time.fixedDeltaTime);


            // Vector3 v = playerTransform.position - Vector3.zero;
            // v += v.normalized * distance;
            // transform.position = Vector3.zero + v;
        }

Thanks to both of you.

Because you’re currently using a capsule you seem to be only concerned with the player’s upright direction. When you start to consider the player’s look direction you may find everything will make more sense and you’ll able to set the camera’s position and look direction with something like this:

    transform.position=playerTransform.position.normalized*distance; // Go directly above the player
    transform.position-=playerTransform.forward*10; // Now go slightly behind the player
    transform.LookAt(playerTransform.position,playerTransform.forward); // Look directly at the player and have the camera's up direction pointing in the player's look direction

works like a charm. The thing i missed was setting the player as LookAt world up.

Thanks

1 Like