Unity Tutorial 03

 









Image from the tutorial

For this weeks Unity tutorial I had to do something very different from the last one where I learned how to control a car and destroy crates. This week the tutorial taught us how to create a game in which you control a farmer and you have to fire pizza slices at incoming animals in order to feed them. To get started I had to follow the same process as the first tutorials and create an empty prototype 2 folder and download the prototype 2 starter files. I had to a human, 1 food and 3 animals to the hierarchy. I dragged in a moose, a fox and a doe just like in the video.  I also had to adjust the XYZ scale so I could see the food clearly from above.  The starting process was very much the same as the one in the first Unity tutorial I did only it is a completely different setting which I found very interesting. I would've assumed that the starting processes would be different depending on your game. I had to create a scripts folder in my assets folder and add a PlayerController script. I then had to add a new horizontalInput public float in the PlayerController.cs. I then had to add Input.GetAxis("Horizontal") in the Update(). This was to get the player to move left and right. Again it was easier this time around because I had already done this before. I then had to add transform.translate(vector3. right) and multiply it by my horizontal input so then the code knows whether the player wants to go positive or negative.  I then added a speed variable of 10.0f;. I then tried a new thing called an If-statement. I had to add it to my update method. I then had to add transform.position.x <-10 to my if-statement. I then had to add a new vector3 of -10 and add a transform.position.y and transform.position.z. I did this to stop the player from moving off screen. Next thing I had to learn how to make the food fly towards the animals. I had to add a MoveForward script and attach it to the food object. I then had to create a new public float speed variable of 40. I then had to add another transform.translate variable and a vector3.forward. I then had to make the food into a prefab. To do this I had to make a prefabs folder and drag my food into it. I then had to add a public GameObject variable and name it projectilePrefab and then drag my food prefab into the variable. I then had to make it so that I could fire the pizza slices using the spacebar. To do this I had to add a new if-statement variable in my update method and call it if (input.GetKeyDown(KeyCode.Space)) {. I then had to add a comment into the if-statement saying Launch a projectile from the player. I then had to add an instantiate method.  I then had to make the animals into prefabs so they could move. To do this I had to give each animal a MoveForward script. I then changed the speed of each animal so that they weren't moving at the same speed. I then had to drag all three animals into a prefabs folder. I then had to destroy the projectiles when they were out of bounds. To do this I had to create a DestroyOutOfBounds script and apply it to the projectile. I then had to create a private float variable called topBound= 30; I then added a new if-statement with a transform.position.z > 50 topBound and then add Destroy(gameObject). I then followed a similar process to destroy the animals offscreen. The next thing I had to do was make each animal spawn automatically. First I had to create an empty object called SpawnManager in the hierarchy. I then had to create a new script with the same name and attach it to the spawn manager and open it. I then had to add animalPrefabs; to the public GameObject variable then I had to go to my inspector and change my array size to match my animal count then assign each animal by dragging them from the project window into the empty slots. Afterwards I learned how to make an animal spawn by pressing S. I had to add an If-statement to my update method. I had to instantiate an animal prefab as well and add a vector3(0,0,20). I then had make a new public integer variable called  animalIndex; Then I learned how to spawn random animals from the array. To do this I had to add the animalIndex; variable into my method. I then added a new variable called Random.Range(0, animalPrefabs.Length); Next up I had to randomize the spawn location so that they wouldn't all spawn in the same place. To do this first I had to change the vector3 to (Random.Range(-20, 20), 0, 20), I then had to make a new vector3 spawnPos variable and paste my Random.Range into it. I then had to create a private float called spawnRangeX = 20; and another one called spawnPosZ=20; I have now learned how to create something other then a driving game in Unity and a way more challenging and interesting game.


Comments

Popular posts from this blog

Introduction Post

My Favourite Game: Grand Theft Auto Franchise

Game Alpha