Set Player Start Position with C# in Unity (part 2)
Next, we obtain the current position of the player and then assign it a new position to start from. This portion of the work will be done with C#. We pick up where we left off in Part 1 where we now have access to the script as a component in the Inspector panel for the object, Player.
We’ll be looking at void Start for the initial position. Here is that section of the script in its default form.
To set the start position we need to start at the highest levels and work our way down to the details. This is a hierarchical structure. In this case we ask for the transform first.
The transform, in Unity’s Inspector, contains everything about the Player object, including what we want, its position.
Next, we need the position on each of its axes, x, y, and z. While it is possible to assign a new position for each axis individually, you would need to define variables for each, as you will see in the tool-tips in Visual Studio if you attempt to go that route. The easiest way to do this in Unity, however, is with a new Vector3.
Then, you can specify the value for each of the axes.
This will set the start position of your player object to 0 for x, y, and z.