Unity Tutorial 04

 


Image from the tutorial

For this weeks Unity tutorial I had to continue on from last weeks lesson where we created the game in which you fire food at animals. This time we learned how to make an animal spawn automatically every couple of seconds instead of them spawning when a key is pressed. I also learned how to make the food and animal disappear from the scene when the food hits the animal. For the first time I had to create a new method called SpawnRandomAnimal () {. The type of method is called a void. I then had to copy and paste my code from my If-statement and paste it into my new void and remove the code from my if-statement. Then I had to add SpawnRandomAnimal (); to my If-statement. To make my animals spawn on a timer instead of spawning when a key is pressed I had to add InvokeRepeating("SpawnRandomAnimal", startDelay, spawnInterval); into my start method. I then had to remove my If-statement so that I no longer had to press S to spawn animals. I then had to two new private floats called startDelay = 2; and spawnInterval = 1.5f. I then had to remove all my spawn code from my update method. I then had to create collider and trigger components so that the food and animals would be destroyed on collision. I had to go into the animal prefabs and add a component called Box Collider. I then had to go into my Box Collider and add an Is Trigger. I had to add my box collider into each animal and the food to make them an Is Trigger. I then had to add a RigidBody component and turn the gravity off so that the food would stay in the air. I then had to create a new script called DetectCollisions.cs and add it to each animal prefab. I then had to add a new private void called OnTriggerEnter. I then had to add in Destroy(gameObject); and Destroy(other.gameObject); After this I learned how to end the game if the animals get past the player and the player loses. To do this I had to go into my DestroyOutOfBounds.cs and add Debug.Log("Game Over!").  After doing this I had to add dogs to my game and balls to fire at the dogs. The balls were supposed to be destroyed when they came into contact with the dog however when I downloaded the assets to do this it was full of bugs. The dogs were spawning in the air instead of on the ground and the balls were spawning on the ground instead of in the air when it should've been the other way round. The dogs were also flying off the screen. Using the steps I've already learned from the previous tutorial I had to fix these bugs. After I finished doing this random balls were spawned from the air for the dogs to catch and I was able to spawn a dog by pressing the spacebar and the balls were destroyed when they collided with the dogs.  The dogs and ball were removed from the scene after leaving the scene and the game was over once the ball hit the ground. 


Image from the tutorial

After I did these previous two tutorials I learned how to make a player jump. The first thing I had to do was create a box collider. Then I had to create a new scripts folder and add a PlayerController script like I have to do at the beginning of each tutorial. I also had to add a RigidBody. I then had to make my RigidBody private in the PlayerController script and name it playerRb; I then had to add playerRb = GetComponent<RigidBody>(); to my start method. I also had to add playerRb.AddForce(Vector3.up * 1000).  I then had to make my character jump by pressing the spacebar. To do this I had to add an If-statement to my update method called if (Input.GetKeyDown(KeyCode.Space)). I then had to remove my AddForce from my start method and add it to my If-statement. I then had to add ForceMode.Impulse to my AddForce and change my AddForce from 1000 to 10 so my player wouldn't jump as high. I then had to create a public float called jumpForce=10; and add jumpForce to my AddForce. I then had to add a new public float called gravityModifier in my start method. I then had to stop the player from double jumping. To do this I had to make a new public float called a bool and call it isOnGround=true; I then had to add && isOnGround to my If-statement and isOnGround = false; I then had to create a private void called OnCollisionEnter(Collision, collision) and set my isOnGround to true. I then had to add in obstacles for the player to jump over and spawn them using methods I had already previously learned. 



Comments

Popular posts from this blog

Introduction Post

My Favourite Game: Grand Theft Auto Franchise

Game Alpha