Setting up the player starting position
Every time our game starts, we should reset all its conditions to the same state. We already mentioned that resetting the starting position of the Player
game object would be a good start. Positions in the 3D world in Unity are described using Vector3 struct
. Go ahead and type Vector3
in the Scripting Reference for a better understanding. This is complex stuff, so don't worry if you can't get it. All you need to know now is that Vector3
is made up of three floats describing x, y, and z positions in the space.
Let's go forward and perform some code changes to set up the Player
position. In PlayerController
, we will do the following:
- Add a private
Vector3
type variable and call itstartingPosition
inPlayerController
. - Assign the
startingPosition
value taken from thePlayer
game object world space position in theAwake
method. This way, we will always store the initial position of thePlayer
game object just after Unity starts executing the game. - Rename the...