This week we started our first game in earnest. Let’s talk about the design and the steps we took to begin implementing it.
The game involves a simple play area with pins. The players tries to knock down the pins by controlling the direction and force at which the ball is projected. The number of pins knocked constitutes the score. The game shares element of classic games such as pinball, bowling and skittles, to name a few.
Setting up the play area
To establish the play area, we used a plane (for the ground) and four cubes stretched to size to provide the walls:
Because we don’t want gaps in our walls, we’ve been careful to be precise with the positions of everything.
Applying a Force to our Sphere
We added a sphere to our scene and made sure that it had a RigidBody body component. The RigidBody component makes it so that the physic engine takes care of the movement of the sphere.
To test adding a force to the sphere we added a temporary script to the sphere and had it add a force to the RigidBody as soon as the game starts.
Immediately it was clear that the force was too weak, so we added a property to the script which allowed us to scale the force. In testing 1000 proved to be a good value. Here is the script:
public class PushSphere : MonoBehaviour
public float PushStrength = 1.0f;
// Use this for initialization
void Start ()
rb = GetComponent<Rigidbody> ();
rb.AddForce (Vector3.back * PushStrength);
// Update is called once per frame
void Update ()
We also experimented with creating a physic material and assigning it to the sphere. Different levels of friction and bounciness give different behaviours.
Testing With a Single Pin
To test the sphere colliding with a pin we added a single cylinder to the scene. Like the sphere, we made sure it had a RigidBody component attached to it. When the game started the ball shot forward and knocked the pin, as we’d hoped.
To aim our sphere and control the strength of our shot, we created an aimer. The aimer is composed of an empty object containing two cylinders, one pointing forwards and one pointing crossways.
We didn’t want the aimer cylinders interacting with anything so we made sure to remove the colliders from the cylinders.
We also wanted the aimer to be partially transparent so we created a new material, setting the Rendering Mode to “Transparent”, choosing a colour from the Albedo colour picker and setting the Alpha value to a value less than 255.
Updated project files for this week’s project can be found here. This requires Unity Version 5.4.1f1 or later.