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

mishmanners

Michelle Duke

Posted on January 15, 2021

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

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.

💖 💪 🙅 🚩
mishmanners
Michelle Duke

Posted on January 15, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related