Course Content
Unity for Beginners
Unity for Beginners
1. Unity Introduction
2. Write your First Script
5. Polishing and Export your Game
Game Improvement
This is the code to make our player come back at it's start position when he lost so let's explain it:
-
Vector2 startPosition;
- This line declares a variable
startPosition
of typeVector2
. It will store the initial position of the player;
- This line declares a variable
-
private void Start()
- This method is called when the game starts. It assigns the current position of the player to
startPosition
, effectively saving the starting position;
- This method is called when the game starts. It assigns the current position of the player to
-
void playerLost()
- This custom method is triggered when the player loses. It resets the player's position to the saved
startPosition
, stops any movement by setting velocity to zero, and logs a message "You lost" to the console.
- This custom method is triggered when the player loses. It resets the player's position to the saved
What’s next:
In the next chapters, we will discuss major improvements in the game, such as adding UI elements and sound effects.
Everything was clear?
Thanks for your feedback!
Section 3. Chapter 5