Variables — A very powerful tool in code

My goal today is to dive into variables, starting off with speed for my Player object.

First, I’m going to examine what variables are and the components within them:

Variables are boxes of information with changeable information. They are either public or private. They have a specific data type: integer, float, boolean(True or False), and string. They all have names. Optionally, they sometimes they have assigned values.

This means the speed will default to 3.5.

So, with this in mind; I’m going to create a speed variable at the top of the script with public float speed = 3.5f;. The f is important for float values, otherwise the code won’t compile. Public means that other game objects can see and affect the variable instead of just the Player object. I’ll also multiply the Vector3 by speed and then by Time.deltaTime to make it so the variable can properly adjust the movement rate.

speed = 3.5
But we can change it within the inspector on the fly.
speed = 10

However, unless there’s a direct need for another game object to interact with your variables, it’s best to keep it private so I’ll change it to private float _speed = 3.5f, using the _ at the beginning to denote a private variable to keep track of what’s private and what’s public at a glance within my code.

In order to continue to access the variable within the inspector, I’ll add [SerializeField] above it.

Next I’ll be looking at the importance of pseudo code and finalizing player movement with WASD.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store