A question about float and f?

public float speed = 2f;

New to Unity, watching this tutorial and he didn’t explain it well.
Still don’t understand what float and f means?

Doesn’t float just mean the number will have a decimal?
What does f mean? Is it just a clarification for Unity telling it, “Hey, this unit will be a float!”?

Or am I completely wrong? And should probably uninstall Unity and donate my computer to the nearest Salvation Army and never buy a computer ever again?

Yep, you got it :slight_smile:

A float is a floating point number (can have numbers less than 1).

It’s recommended to be explicit about the types of numbers you use, in case you mix up floats and ints. It’s of course not so much of a problem in declarations that also set a variable, but if you declare “public float speed;” first, then set it based on some criteria later in Awake() or Start() you’re best off saying that you expect it to be a float.

It’s a useful hint to the reader, which normally is future you, who doesn’t remember what the hell past you was on about. Some compilers/languages can be strict about the types too, so you have to specify that a number is a float.

You’ve basically got it. A float is a ‘floating point number’; the way the data is stored is similar to scientific notation (6 x 10^23 is a huge number, but can be stored with two integers, 6 and 23; floats are like that, but they do it in base 2 instead of base 10).

The reason that “f” is required is due to the existence of the type “double”. Double is also a floating point number, however, it’s double precision - it contains twice as many bits. Because it’s more precise, in many applications, it’s more commonly used than the float type. However, in video games, CPU time and memory are at a premium, so floats became the “standard non-whole number” format. (And because it was the standard, gaming hardware is specialized at dealing with floats, so it has become sort of a legacy thing, even though 64-bit chips are common now.)

So in C#, when the compiler sees 1.33452, it by default assumes that you want a double “because hey, who wouldn’t prefer a double?” The “f” tells it that you want the lower-precision, “faster” (sort-of) option.

1 Like

There’s more than one type of decimal variable, so you have to say which one. There’s double with a precision of 16 or float with 7. To specify a literal eg, foo = 10.5 means double. foo = 10.5f means float

One more question please!

public float speed = 2f;

transform.Translate (new Vector3 (speed, 0, transform.position.z) * Time.deltaTime);

The transform just means you are about to change something.
Translate means you are going to move something from point A to point B.
new represents a new (e.g. Vector)
Vector 3 (the 3 just means the vector will have 3 units and not a representation for a new vector)
speed is our X, 0 is our Y and transform.position.z is our Z and the Z will remain constant)

Time, this is Unity’s main clock
deltaTime means it will keep track of the clock every render.

Is this all correct? Please reply!

This is the only part you significantly got wrong. The Transform is the component that represents your object’s position and orientation in space. The transform in this line indicates that you’re about to reach into that component, not that you’re about to change something.