Lesson 3.2 - Make the World Whiz By
Create a script to repeat background
- Create a new script called RepeatBackground.cs and attach it to the Background Object
Reset position of background
- Declare a new variable private Vector3 startPos;
- In Start(), set the startPos variable to its actual starting position by assigning it = transform.position;
- In Update(), write an if-statement to reset position if it moves a certain distance
Fix background repeat with collider
- In PlayerController.cs, declare a new private Rigidbody playerRb; variable
- In Start(), initialize playerRb = GetComponent<Rigidbody>();
- In Start(), use the AddForce method to make the player jump at the start of the game
Make player jump if spacebar pressed
- Add a Box Collider component to the Background
- Declare a new private float repeatWidth variable
- In Start(), get the width of the box collider, divided by 2
- Incorporate the repeatWidth variable into the repeat function
Add a new game over trigger
- In the inspector, add a “Ground” tag to the Ground and an “Obstacle” tag to the Obstacle prefab
- In PlayerController, declare a new public bool gameOver;
- In OnCollisionEnter, add the if-else statement to test if player collided with the “Ground” or an “Obstacle”
- If they collided with the “Ground”, set isOnGround = true, and if they collide with an “Obstacle”, set gameOver = true
Stop MoveLeft on gameOver
- In MoveLeft.cs, declare a new private PlayerController playerControllerScript;
- In Start(), initialize it by finding the Player and getting the PlayerController component
- Wrap the translate method in an if-statement checking if game is not over
Stop obstacle spawning on gameOver
- In SpawnManager.cs, get a reference to the playerControllerScript using the same technique you did in MoveLeft.cs
- Add a condition to only instantiate objects if gameOver == false
Destroy obstacles that exit bounds
- In MoveLeft, in Update(); write an if-statement to Destroy Obstacles if their position is less than a leftBound variable
- Add any comments you need to make your code more readable