DEV Community

loading...
Play Button Pause Button

When coding C#, you need to use f for timeScale

Michelle Mannering
Developer Relations | Hackathon Queen | Community Manager
・2 min read

When working with C#, I found out you can't use seconds for time. Why? I have no idea! Well I had no idea. If you want to slow things down or speed things up, surely you use actual time right?

At first when I tried to code this up, I wasn't using any units. I did however try "s" for seconds which didn't work. I tried it without units, and then I discovered you need to use "f". So I plugged it into my code, and presto!

It works! I love it when things just work. So what's the logic here?

When I was making my character move faster, I simply used 2.0. There was no need to add any units. Same for when my character was running in real-time. I only needed 1.0, no units required. But when working with decimals, it's different.

TimeScale in Unity

For timing you can use decimal points and 0.X in order to slow down the movement of your character by fractions. However when working in decimal points, you need to add the f. F stands for frames. For example, 0.5f means the movement will go at half the real-time (or normal) speed. Normal speed is 1.0 (note you don't need the f although it's good practice to add it anyway).

0.25f is a quarter of real-time speed. The reason time doesn't work in seconds is because Unity and C# use delta time according to a timescale rather than a single point in time. It's relative. This is magical, because you can fix the time based on another frame rate. It's really awesome and perfect for gaming ie. something happens after something else happens; a spell in Elder Scrolls Online occurring X time after being hit as an example.

If you want to read more about Unity's TimeScale check out the Unity Docs. I am really loving learning all things Unity and C# and can't wait until I finish the Unity C# Survival Guide.

b2a9c1f3-5543-4e31-9c0d-4bdfdc38a02f_unityblank

Join the learning journey

What do you want to learn with Unity? What should I learn next?

If you want to follow my coding journey, give me a ❤️ on Twitch. You can also check out one of my latest Unity streams for lots of fun (and a bit of frustration). It's so exciting seeing things work in code, and I can't wait to dive back into live coding for 2021! See you soon.

Discussion (1)

Collapse
jayjeckel profile image
Jay Jeckel

The f is called a literal suffix and it doesn't mean "frame" it means float and it tells the compiler to treat the number literal as a float type. You have to use the f suffix because Time.timeScale is a float typed variable and double type literals can't be automatically converted to float.

// Works, because 1 is an int and int auto-casts
// to float because float is bigger than int.
Time.timeScale = 1;

// Doesn't work, because 1.0 is a double and double
// won't auto-cast to float since float is smaller than double.
Time.timeScale = 1.0;

// Works, because 1.0f is a float and doesn't need to be cast.
Time.timeScale = 1.0f;

// Works, despite 1.0 being a double because we are
// explicitly casting it to a float.
Time.timeScale = (float)1.0;
Enter fullscreen mode Exit fullscreen mode

It has nothing to do with Unity, units, or time scales, it's just basic operation of a strongly typed language.