Part 29 - Some more changes.
I'm going to skim over the changes I've added. By now it should be apparent how things are done... These are slow progressive changes that aren't that complex, or shouldn't be that complex to do now...
I've added a lip like the bottom floor. So coins will pile up like they do on the bottom. This also required moving the InitiaCoinSpawner up by 0.2
The animation. I swapped around the extremes, so it now starts at 3 and goes to 5, and comes back out to 3. This is so that the coins that are initially spawned will land on the top and are pushed down onto the lower platform. Leaving a few on top.
I finally decided that -1.5 is the best place for it to spawn from. But that's also because…
I moved this back by 0.2, so there is only a gap of 0.2 between it and the back wall. This means the coin has to fit in that small gap. It helps prevent the coins jamming as much as possible.
Ultimately it would be best that this gap was as thick as the coins, but the physics engine probably wouldn't like it. And in reality it isn't like this anyway.
I'm going to add a Quincunx to the machine now.
I've added a pattern like the 5 side of a die for the coins to bounce around as they go down, made from small cylinders.
This gives this…
The problem now is this happens a lot.
I think there are multiple problems going on here.
The coins jam against each other very easily. The friction is far too high.
We need to give the coins a material with a low friction.
Create a new "Physic Material" in the Assets folder, I called it "Low Friction" and assign it values of 0.1 for dynamic friction and 0.4 for static friction. These mean it has low friction when moving, and only slightly higher friction when not moving, so they won't stick together.
This alone seems to solve the problem so it might be enough.
The other problems that might need fixing in the future are the coins not being round and the glass front wanting to be moved out slightly at the bottom. But at the moment they aren't causing a problem.
Coins are still getting stuck, when it gets stupidly congested…
I think the next thing to do will be to change the shape of the coin to be more rounded.
Using the ProBuilder tools I created a new 64-segment cylinder, of width 0.5, and height 0.1. So the same size of the original coin, but with more sides making it up, so far rounder.
Dragging this new shape over the "coin" prefab, it replaces the existing one, but it now needs 'fixing'.
This involves changing "static" back not unticked in the top.
Adding back the RigidBody as this had been removed.
Adding back the
Setting "Interpolate" as the interpolate method in the RigidBody.
And finally using ProBuilder to set the material to the "coin" material.
It's still getting jammed…
But the coins are prettier.
I think the next thing will be to make sure coins don't overlap by making that gap between glass and wall as small as possible.
Manually altering the Glass wall to a Z of -0.12 and the SpawnLocation to -0.11 puts these as tight as it allows before it stops spawning.
The coins at present can fall and pile vertically on top of each other fairly easily.
In reality, the coins would have some randomness of rotation and slight offset when they fall. But at present they are perfectly still when they are created.
I am going to add a little jitter to the velocity and angular velocity of the coins when they spawn, so they are moving slightly and rotating slightly. To give a slight amount of realism to them.
First we need some configurable
Jitter properties in the
public float velocityJitter = 1.0f; public float angularVelocityJitter = 10.0f;
Now in the SpawnObject function, after we have created and positioned the coin, we need to get it's Rigidbody component and add some randomness based on these Jitter values…
var coinRigidbody = coin.GetComponent(); coinRigidbody.angularVelocity = Random.Range(-angularVelocityJitter, angularVelocityJitter) * coin.transform.up; coinRigidbody.velocity = coin.transform.forward * Random.Range(-velocityJitter, velocityJitter) + coin.transform.right * Random.Range(-velocityJitter, velocityJitter) ;
If you look at the coin in the inspector you can work out the directions that things rotate around and need to move around based on the arrows.
coin under the "Spawn Location" object in the hierarchy we see
With the green arrow showing that the coins
up vector corresponding to the vector we will want to set the initial rotation around. While the red arrow being the
right vector and the blue arrow being the forward vector.
So from that, in the above code, we create a random angular velocity around the coins
Then we create a random velocity in the "right/forward" plane.
Both relative to the objects frame of reference, not the 'world'.
Finally for this section, extending the top of that back wall, so that coins don't fall off the back.
Using the Probuilder tools to select the top of the wall
Then using the Extrude faces option…
To add a new face extending from the existing one
Moving the wall up…
And finally pushing that top edge back
This can highlight a problem still…
Coins getting jammed at the top. A better way would be to have an actual chute delivering the coins instead of them just materialising in the gap.
But, buggy as it is, it still needs committing… but no tag…
… it's not finished…
This has been an experiment… but to play with it for a bit, the best solution I've found so far is to create a solid cube, across the top of the entire possible spawning area, and add the
SpawnerBlockerDetector script to it. to stop any coins being added until the entire area is clear.
Which looks something like
Also, adjusting the physics material to having a friction values of both 0.1 helped some of the jamming at the bottom of the wall.
Cooking the mesh doesn't help at all. So in the coin prefab, I've turned off all the options in the Mesh Collider for "Cooking Options".
I've changed the coin, so it's half the size, but replaced the coin mesh...
Like before, when changing the resolution of the cylinder, I created a new cylinder using probuilder using the following options..
To add more points for collision to the coin.
"Build cylinder" and set it's position to 0,0,0. Then dragged it over the coin prefab, replaced it and set up the details of the prefab again. Added the texture, added the Rigidbody, Set
Pb_Entity "entity type" to
Mover. Set the Mesh Collider material and removed the cooking options. Set it to a Convex collider…
There are some problems with the coins being flat falling through the floor. I've updated the
InitialCoinSpawner to add a random rotation to the coins. This is a limitation of the physics engine, not the code directly.
I also extended the
InitialCoinSpanwer object out to cover more of the lower platform and increased the number of coins spawned to 200.
You can see the final repository here: https://bitbucket.org/hiveit/unity-tutorial-coin/src/83ea7060bab79c8a46d5c55fe0b48f723f7c9b09/
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 13 - It is still too fast
- Part 14 - Coins are piling up
- Part 15 - Materials
- 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