Unity Create with code Unit 2

Unit 2 of the Create with code tutorial required you to make a mini game whereby a player walked round and fed AI animals.

The first thing was to code the horizontal movement of the player, this could be done in 3 ways, either by defining the numbers outside of a variable (which may cause problems further along in the process) or by adding variables.
by adding a right and left boundary, the player no longer fell off of the edge of the map. I then cleaned the code up by just adding one variable in instead of using both right and left boundary.

This cleaned my code up, made it easier to access and also made it less prone to bugs and glitches later on in the process.
To ensure when the food was thrown and the animals run they don't continue on for all eternity, I added a destroy object script, this means that when the animals or the food gets to a certain point on the scene, it will be destroyed, this prevents entities compiling and making the game run slowly.
The next step was to ensure that when animals spawned, they spawned randomly, to do this I used a 'Create Empty' and applied my Spawn manager script to this, this in turn made it so that every time 'S' was pressed animals spawned in random places between -20 and 20 on the scene. Once this worked, i needed to make sure that animals spawned by themselves so that the player didn't have to press 'S' every time they wanted an animal to spawn, here is the code for that:
I then added box colliders to all of my animal prefabs and added both a box collider and a rigid body to my food prefab so i could code reactions later on.

I then made a detect collision script, this script made it so that when the food and the animal's box colliders hit each other, they both despawned.
I then got the game to print out "Game Over" if an animal gets past the player and goes out of bounds.









Comments

Popular Posts