Unity Tutorial 05


Image from the tutorial

For this weeks Unity tutorial I had to continue on from last weeks in which I made the character in the image above move along the screen and jump over obstacles when the spacebar is pressed however once the character gets to a certain point the background disappears so I had to make the background move along with the character. I wasn't able to duplicate the background images because eventually the image slides would've run out so instead I reset my background and added two background slides right beside each other and created a new script called RepeatBackground.cs and added it to my Background Object folder. I then had to add a private Vector3 variable to my background script and call it startPos; I then had to go to my Start method and set the startPos variable to transform.position; I then had to add an if-statement in my Update method called if(transform.position.x < startPos.x-50). I then had to add transform.position = startPos; to my if-statement.  My background still looked funny after doing this so I had to fix it. In order to do this I had to add a box collider component to my background to make it wrap the whole way around the background which made it look better. I then had to add a private float variable to my RepeatBackground script called repeatWidth; I then had to add repeatWidth = GetComponent<BoxCollider>().size.x / 2; to my Start method and change my if-statement to if(transform.position.x < startPos.x- repeatWidth). I then had to create a game over trigger for when the player collided with an obstacle. To do this I had to add a ground tag to my inspector and an obstacle tag to my obstacle prefab. I then had to add a public bool called gameOver; to my script. I then had to add an if-statement to my OnCollisionEnter variable called if(collision.gameObject.CompareTag("Ground")). I then had to move my isOnGround = true; variable from my OnCollisionEnter void to my if-statement. I then had to add another if-statement called 
else if(collision.gameObject.CompareTag("Obstacle")). I then had to add Debug.Log("Game Over"); and gameOver = true; to my if-statement. Unfortunately my background and obstacles were still moving when game over came up so of course I had to change this. To do this I had to add a new private PlayerController to my MoveLeft class simply called playerControllerScript; I then had to add playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>(); to my Start method. I then had to add an if-statement to my Update method called if(playerControllerScript.gameover == false) and I had to add my translate method to my if-statement. I then had to stop objects from spawning after I reached game over. To do this I needed to go to my SpawnManager script and use the same code I did in the MoveLeft script. I then had to destroy any of the obstacles that left the screen using the same code I did for last weeks tutorial.
 

Comments

Popular posts from this blog

Introduction Post

My Favourite Game: Grand Theft Auto Franchise

Game Alpha