Making a 3rd person camera on a spherical world

In my previous blog we covered how to set up our own spherical gravity. This works great if you have a ‘birds eye’ view of the scene, like watching a moon orbit a planet, but creates a problem if you are trying to view your scene from 1st or 3rd person views using traditional unity camera control schemes, namely they tend to flip upside down, rotate around the player and always look backwards at them or just flip about uncontrollably. This will disorient and frustrate the player and just ruin a game experience completely.

This may get a bit technical (read: confusing / poorly described), so please don’t hesitate to comment with any questions you may have.

When creating a camera control script, Transform.LookAt() is commonly used. It works brilliantly, as you are able to set the up direction that you use, even dynamically. Instead of using the world’s up position, we can grab the up position of the target we are trying to follow (or negative forward position if you are using my spherical gravity example, as an object will look towards the center of mass).

If instead of making the camera a child of the player object, we just make it follow an ‘anchor’ that is used to orbit the camera around the player, using it to always face towards the center of mass, copying the player’s position in space (not as a child, so it does not inherit rotation or scale) and having the camera follow that anchor (through transform.position = new Vector3 (player.position.x,player.position.y,player.position.z + offsets)) we can also rotate the player in any direction without affecting the camera’s rotation. You could also add in a Lerp to help smooth out the camera movement, nice and subtle, to help give your game a good vibe.

For the player control and camera in Clump Soul, I used a 3 part system:

Player, Anchor, and Camera.

  • The Player provides only the position in space and is affected by gravity.
  • The Anchor provides rotation, used to change the direction of movement, the dynamic upwards direction for the camera to use and the offset positions for the camera to follow. This is also what the player was actually controlling.
  • The Camera always looks at the player and moved smoothly into new positions.

Here is an in depth example of how to set up different types of cameras to work on a flat plane, including some code examples.

11 thoughts on “Making a 3rd person camera on a spherical world

  1. Duan van Staden

    Hi
    I’m currently working on a similar project that requires a third person camera rotating around the player using a sphere like gravity system. Currently the problem is that the camera is following the anchor of the planet but the player’s rotation gets messed up on the bottom of the planet. I was wondering if you have any methods or tips on how to achieve a similar effect such as the game “Grow Up” does?

    Like

    1. I had the same issue when I hit the bottom of the planet. IIRC, the problem lied with gimbal lock, because I was not using quaternions to rotate. have a look into gimbal lock, and see if you find the solution there!

      Like

      1. Duan van Staden

        Hi Ben,
        You talked about the anchor providing the rotation for the camera, but how can the camera be rotated in the X axis if the X and Z are constraint on the anchor?

        I have tried numerous versions of this camera controller but don’t seem to succeed in any of them.

        Can you please explain a little more in depth please how you have done this?

        Like

    2. So what is actually happening here is that the x and z axis are used to align the player and camera with the gravity of the sphere. The easiest way to get this to happen is by using the lookAt function, and using an anchor to only get the two axis that we need. The y axis is used to rotate left and right. a 3rd person camera doesn’t need to have the player centered at all times, so you can just use the y axis result of a lookAt function that is focused on the player. There is definitely a more elegant solution by just working this out in code, but this was simpler for me to understand when I wrote it.

      Like

  2. Kim Vasquez

    what do you mean by anchor? is it a new empty game object? So do I need to create an empty game object and make it as a child of the player and the camera(with follow script. not a child of player) follows the new empty game object?. Please help. Thank you!.

    Like

  3. André Marques

    Hey, I have some doubts what does the anchor stand for, I mean what is the anchor purpose.
    I know you explained above but I didn’t quite understand it, i was hoping that you could explain.

    Like

Leave a comment