Part 14 - Coins are piling up.

By Jamie Chatterton / 2018-06-12

Coins will start to pile up on top of the pusher now.

It's a consequence of the coins falling from where they are, the speed of the pusher, and nothing sweeping up.

Maybe we start with the easy thing? Adding something to push coins off the top?

Create a duplicate of the floor call it, I dunno? 'wall'? Rotate it. Now this could be done by hand, but it's probably best to do it in the Transform of the Inspector. Change the "X" rotation to be "90" so 90 degrees around the "X" axis. It'll now be a vertical 'wall'.

Now move it using the "Move Tool", so it sits on top of the pusher object. You may need to zoom out with the screen wheel to see all this.

Open this image in a new tabCreating wall
Creating wall

The Object is defined as "Static" so it will remain in place. It has all the physics it requires as it's based on an object that already responds to physics and that the coins hit.

Press play.

That's doing its job quite nicely.

But it's a bit big… use the ProBuilder tools to shrink it a little…

Open this image in a new tabShrink the wall
Shrink the wall
Open this image in a new tabShrink it!
Shrink it!

Something's wrong though… the coins are just landing in the same spot… be nice if there was some variation.

This is fairly simple. For now let's just add some randomness to where the coin is placed.

The Instantiate function returns the instance it created. We can manipulate it once it has been created. Note, instantiate doesn't actually create the object there-and-then, we can manipulate some of the values and it will be finalised later.

So, in the SpawnObject function, we want to assign the returned value from Instantiate to a variable and then manipulate the position on its transform object. The "transform" being the transformation in the world, holding position and rotation (orientation). The position object is a Vector3 object, with its X, Y, Z coordinates.

Looking from above we have

Open this image in a new tabAdding randomness
Adding randomness

Using the grid pattern that ProBuilder applies to objects as a guide, each square in an object being a length of 1 unit. It should be possible to ascertain that the width of the platform as-is is 6-units across . The center of the pusher is aligned with the middle of the platform. If we want the coins to spawn evenly from left-to-right we need to spawn from negative to positive evenly.

Also, we need to take into account the width of the coin, which is 1 unit.

So we essentially need to randomly go from -2.5 to 2.5 in the X direction when adding a coin to the scene. This value will get added to the position of the pusher, we don't need to do this ourselves if we do this correctly.

Unity provides a local of utility functions, we once would've had to have written a function to calculate random number ranges, but we have that provided now.

Random.Range( from, to )

The above line does this for us.

Instead of manipulating the position of the transform object, we manipulate the localPosition. position refers to the world position and will not take into account the parent offset. localPosition will.

So with all this taken into account, we can change the SpawnObject function into…

void SpawnObject() {
	var coinInstance = Instantiate(ObjectToSpawn, transform);

	var position = coinInstance.transform.localPosition;

	position.x = Random.Range(-2.5f, 2.5f);

	coinInstance.transform.localPosition = position;

And we have...

Git repository:

More posts in this series.