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 variablestartPosition
of typeVector2
. It will store the initial position of the player; -
private void Start()
: this method is called when the game starts. It assigns the current position of the player tostartPosition
, effectively saving the starting position; -
void playerLost()
: this custom method is triggered when the player loses. It resets the player's position to the savedstartPosition
, stops any movement by setting velocity to zero, and logs a message "You lost
" to the console.
Everything was clear?
Thanks for your feedback!
Section 3. Chapter 5