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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s