Variables for Speed in Unity

David Little
3 min readApr 2, 2021

Even though we have been able to set a speed using Time.deltaTime, we want to be able to control that speed from outside of the script. Think of Time.deltaTime as the throttle on the engine that is the script. We need to add a gas pedal to that throttle to control it more finely from outside of the engine bay. We will do this with a variable.

First thing you will do is determine whether or not other scripts in the game can see the variable. This is called the reference. You will dictate if it is ‘public’ or ‘private’. Private means that the variable is only accessible to the script in which it resides. Public means the variable is accessible to all other parts of the game. You can also use [SerializeField] attribute, where you want the variable to be private but still accessible to devs inside the Unity editor.

Next, there are different types of variables when using C# in Unity. The varieties include the following.
[int] short for integer, which are whole number values, like; 1, 2, 3, -1,…
[float] which are numbers with decimal equivalents, such as 9.2, 1.05, etc. This category also includes irrational and transcendental numbers like 2, pi, and phi (the golden ratio). ‘float’ stands for floating point, interference to the varying position of the decimal point and digits.
[bool] for Boolean, are variables that are either true or false, a one or a zero.
[string] are for variables that contain anything else, characters, lists of words, sentences, etc.

You will then name your variable and then use that variable name further on in your script where it is needed in a method. Note that private variable names customarily use an underscore as the prefix, a .Net standard, which makes it easier to identify private variables when searching through lines of code.

When using a variable you will first declare it, which looks a bit like this…

This example contains everything talked about above. It is private, uses the Serialize attribute to expose it in Unity, is private, and a float value. The only new thing you might notice here is that float values have an ‘f’ as a sufix to further indicate that they are float values.

Lastly you will then use the variable in your script’s methods. This can be done in many way depending on how you build your script. But for this example we will show it being used to define vertical and horizontal speed resulting from player input from a keyboard. And, that looks like this…

For completeness, and to tie all of this together, the entire script looks like this…

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response