Part 14 - Coins are piling up.
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.
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…
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
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: https://bitbucket.org/hiveit/unity-tutorial-coin/src/010_AddingSpawnerRandomness/
More posts in this series.
- Part 01 - Installation
- Part 02 - Concepts
- Part 03 - Navigation
- Part 04 - Git and Unity
- Part 05 - Building a project
- Part 06 - Adding some plugins
- Part 07 - Creating some objects
- Part 08 - Physics
- Part 09 - Pushing things & animation
- Part 10 - Pushing coins
- Part 11 - Custom Scripting
- Part 12 - Spawning things
- Part 16 - User control
- Part 17 - Keyboard input - Fire
- Part 18 - Keyboard input - Movement
- Part 19 - Deleting Coins
- Part 20 - Walls
- Part 21 - Adding a GUI
- Part 22 - Adding the GameController
- Part 23 - Scoring links with events
- Part 24 - Refining the Game - Part 1
- Part 25 - Refining the Game - Part 2
- Part 26 - Refining the Game - Part 3
- Part 27 - More refinements
- Part 28 - Slowing the user down
- Part 29 - Some more changes