Techshop

Unity Tutorial 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...

The top pusher.

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.

The coin

I finally decided that -1.5 is the best place for it to spawn from. But that's also because…

The back clear panel…

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.

Quincunx

I'm going to add a Quincunx to the machine now.

This is the pattern that makes coins 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 "Material" to LowFriction.

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.

GIT 0dbed41

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 SpawnerController

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.

Selecting the "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 "up"

Then we create a random velocity in the "right/forward" plane.

Both relative to the objects frame of reference, not the 'world'.

GIT TAG 025_AddingSomeSlightRealism

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…

GIT commit 7f1a26e

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.

git commit cc07a21

Unity - A simple tutorial < Unity Tutorial 28 - Slowing the user down