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.

How to create spherical gravity in Unity

In my previous blog posts, I talked about a project I was working on, a Katamari Damacy clone, called Clump Soul. What I chose to do was use a sphere for the play area instead of a plane, or the built in terrain that unity uses.

If you are here looking for instructions on creating a 3D world using unity’s built in terrain generator, let me tell you now, it is not built for that, and you will create a near endless amount of work for yourself trying to make it do this. If this does not deter you, check here, here or here. If that wont work for you, then use a smooth sphere as your terrain and just place your own grass and trees, or even create your own sphere with hills on it in Blender, for example. What I’m going to go over here is the problem of gravity and the player camera.

There are two main problems to work out when making a controllable character that will move across a spherical world. The spherical gravity and a controllable 3rd person camera that will orbit the player smoothly, while keeping them centered.

Problem #1: unity physics uses linear gravity, and has no spherical gravity built into it. What you need to do is have a point in space that everything is drawn towards, and treat that direction as downwards, so that each object also rotates as it travels around the source of gravity.

Think of v as the forward direction, and a as the downward force of gravity
Think of v as the forward direction, and a as the downward force of gravity

To the right is a visual example of what we want to achieve. The black orb is moving around the planet’s surface (or large ring around the planet which I am pretending is the surface), and is always pulled towards the center.

To achieve this, the first thing we need to do, is create the gravity. There are two types we can have, gravity that increases in strength as you get closer to the center of mass (great for a game featuring multiple celestial bodies, like KSP), and gravity that is applied at the same rate no matter the distance from the center of mass (like in Super Mario Galaxy). Picking the right one is very important, but the difference in the code is minimal, a diminishing gravity strength can be applied based on the distance between the object and the center of mass.

rigidbody.AddForce((planet.position - transform.position).normalized * acceleration);

What this code does is add force to the object this script is attached to. That force is in the direction of the ‘planet’ (get the gameobject’s transform for this), because we get a magnitude by subtracting the objects’ current point in space from the planets point in space. This is automatically normalized, and we then multiply it by acceleration (any number you put in, this will be how fast you are pulled towards the planet).

Now, getting the object to treat it as down (as if it is falling towards it).


What this does is rotate the object to face towards the planet, using the same method to get a magnitude as before for the direction we want to face. We also use the objects’ own up direction for the new up direction, this stops the object flipping upside down when we move to the bottom of the planet.

In my next post, I will cover the spherical planet 3rd person camera.

APA’s for spherical terrain links:

Spherical procedural terrain shader based on slope. (n.d.). Retrieved July 21, 2015.

Procedural planets. (n.d.). Retrieved July 21, 2015.

Games development & Social Media: all the links are here.

Social media tips from the pros

Social medias are fantastic tools for letting people know about you. What I can provide can be shown on, which is one of the leading platforms for delivering indie games (here is my account). The problem, however, is that I need to get hundreds of thousands of people to buy my games, every year, if I want to make a living off just that and that’s before thinking about the costs that would go into making my own games. What about if I want to make larger titles?

This is where sites like Linkedin or come in handy, networking sites that are focused on connecting you with other professionals, rather than just spreading opinions and advertising. Some very important guidelines on how to appear professional online and appropriate places to host your ‘professional image’ can be seen here:

In the video they mention both online and offline spaces to be aware of. Here in Brisbane there is several MeetUp groups (try here and here). MeetUp is a fantastic resource to find like minded people, and is one of the most important tools for a games developer/programmer. There you will be able to meet people from your industry face to face, showing who you are and what you are capable of much easier than over electronic media. You can also impress people who might be able to get you into a job. In this next video from the SMITE studios, they cover these topics as well, also suggesting maintaining a social media presence and joining and physical events nearby:

Always remember that having a huge ego online (or at least in the public eye) will cause you a huge amount of pain, especially in the gaming industry. as most of your income & job offers stem directly from your online and public presence, if you ruin how others see you, you will starve. Read up here about some brilliant ways to help avoid alienating your audience or here about pissing off your cubicle bound coworkers (these guidelines can be loosely applied here too).

On Phil Fish, Satchbag comments on the complications of Phil’s social interactions and mannerisms, on who he was to his audience, what that cost him, and on the issue between our professional selves and our private selves:

To me, this shows that there is a lot more work involved in this industry than what the general public believes or knows.