Understanding Order of Operations & Making Starting Locations For Players

Kenny Pruitt
Unity Coder Corner
Published in
5 min readFeb 18, 2023

Objective: Creating A Starting Position For A Player

With a Player object already created and a Player script assigned to it already, we can start creating functions for the Player in the game. First we can start by creating a starting position for the player. To do so, first open your Player script by double clicking on it to open it in Visual Studio. Then in the Start function we can assign a new starting position by first getting the current position of the Player, then assigning it a new position.

When starting with C# in Unity, it’s good to have a basic understanding of the order of operation that Unity uses. Not a full understanding of it is needed right away, but just some of the key points are good to know beforehand such as: Awake, Start, FixedUpdate, Update, LateUpdate, and OnDestroy. These operations always happen in order, so when you first start your game the Awake functions will be ran first, then Start functions, and so on. You can see the full order of operation in the picture below.

It is also important to note that functions like FixedUpdate, Update, and LateUpdate are run every frame, so it’s a good idea not to add things into these that you don’t want the game to be constantly running. For example, if you create your Player starting position in the Update function, the player will be locked into that position because every frame the game runs, it will be setting the player to that position making it impossible for them to ever move from that spot. So when creating our Player starting position it is a good idea to place this code into the Start function.

Also it is important to note that whenever you put “//” before anything, you are making a comment in your code that acts as a note for you. This is not something that will affect your code or will be run in your Unity project.

Now that we understand where we must write our code for our Player starting position, the next thing we need to do is get our current Players position. Since our Player script is on our Player object, to get our current Players position all we have to do is access the Players Transform component by typing “transform.” Then to specifically get the position from the Transform, you would type “transform.position”.

Now that we have our Players position we can type “=” to assign a new position to the player. The Players position is stored in a Vector3 format, which is variable x, y, and z. When creating a new Vector3 it would be shown like this: Vector3(x, y, z). So to alter our Players current position, you could simply type “transform.position = new Vector3(0, 0, 0);. An important note here is whenever you finish a line of code you always end it with “;”.

Now with that done, you can save your script, go back into Unity and move your Player anywhere you want and when you press the Play button, your Player will be moved to the starting location that you just created.

Two important things to note here as well:

First, ff you were not sure or you wanted other people to be able to adjust the starting location of the Player, you could create a variable that can be adjusted in your Inspector window. To do so, under your class function type in “[SerializeField] private Vector3 startingPos;”. A private variable is something that other scripts will not be able to adjust to avoid errors, and SerializeField makes that private variable viewable in the Inspector window.

Now if you save your script and go back to Unity, you will see that you have a Vector3 variable that you can assign on your Player script.

Then for this variable to take effect on your Player starting position you would need to change your original starting position code and update it to assign this variable, like so “transform.position = startingPos;”

And Secondly, it is also important to understand “World” space vs “Local” space. If your Player was attached to something, for example, say your Player was riding a horse. The horse is a separate object from the Player, but your Player would be attached to the horse, this way if the horse moves you player moves with it and doesn’t stay in the same position. In this case, the horse would be considered a Parent object and the Player would be considered a Child object. In your Hierarchy it would look like the picture below.

If you were then to assign your starting position the Player, the player would move to world Vector3 position and not with the horse object. If you wanted to assign a starting position based on your local position of sitting on the horse, you would use transform.localPostion instead which would look like this “transform.localPosition = new Vector3(0,0,0);”.

--

--

Kenny Pruitt
Unity Coder Corner

Unity Game Developer, C# Developer, and Software Engineer