Part 28 - Slowing the user down.
Coins jamming…
So, at present the user can just spam the coins button. And the coins jam up.
This isn't very realistic.
The Spawner
should prevent the user from adding a new coin until the area for a coin is free.
First I notice that the coin prefab has had it's position moved for some reason, so resetting that to 0,0,0 helps a lot.
Now add that to the Spawn Location as a child, so it appears in the position that coins spawn from, in exactly the same position.
Next, from this coin, turn off the "Use Gravity" in the "Rigidbody", and turn on the "Is Trigger" in the "Mesh Collider".
We want to use the trigger functions to determine when something has started and finished touching the mesh and to work out when the areas is clear to spawn a new coin. Which is why I used a coin that will be in the same shape.
I've created a Component call SpawnerBlockedDetector
…
public class SpawnerBlockedDetector : MonoBehaviour {
public UnityEvent OnSpawnerBlocked = new UnityEvent();
public UnityEvent OnSpawerClear = new UnityEvent();
private void OnTriggerEnter(Collider other) {
OnSpawnerBlocked.Invoke();
}
private void OnTriggerExit(Collider other) {
OnSpawerClear.Invoke();
}
}
It just passes on the OnTriggerEnter
and OnTriggerExit
events to a UnityEvent that can be bound in the IDE.
In the spawner
object, in it's SpanwerController
Component, I added a new "Spanwer Blocked" bool and altered the SpawnObject
code to only allow objects to be spawned if it is set to false…
public bool spawnerBlocked = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1")) {
SpawnObject();
}
}
void SpawnObject() {
if (!spawnerBlocked) {
var coin = Instantiate(ObjectToSpawn, transform);
coin.transform.position = SpawnLocation.position;
coin.transform.rotation = SpawnLocation.rotation;
OnSpawnCoin.Invoke();
}
}
public void SetSpawnerBlocked(bool value) {
spawnerBlocked = value;
}
And in the IDE, in the 'coin', with the SpawnerBlockedDetector
script, I get it to call this SetSpawnerBlocked
with true or false values, depending on the situation…
Now… when you try and spam the 'fire' button it kinda mostly works. But to help it a little bit more, I also moved the Spawn Location 'Y' value to -0.2 to make sure the coins weren't catching and if there was any slight overlap, that they had a little wiggle room…
Also, finally, the coin, I disabled the "Mesh Renderer" so it's not visible, but is still firing the trigger…
Now even when trying your hardest, it doesn't let you go wild with the fire button… it can still jam, just not as easily…
Git repository: https://bitbucket.org/hiveit/unity-tutorial-coin/src/630a267a9bd2183dd8673b7e855c20836eb5cedd/
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