Design a site like this with WordPress.com
Get started

Day 48: New Behaviour Script

If you want to change the default new script when you Create -> C# Script in Unity, follow the path ~\Unity\Editor\Data\Resources\ScriptTemplates and edit the text file named 81-C# Script-NewBehaviourScript.cs.txt

Sorry, I don’t know what the path is on Mac

Note: You may need to run Notepad as an administrator if you get the ‘Access is Denied’ error message.

With the text file open, you can now edit the default new script.

I deleted the comment lines because they were unnecessary and felt too cluttered. As I get more experienced and see what functions I use frequently in a project, I can tweak it further and save some time.

Current Progress Status

  • Tower Defense Tutorial: 47/73 -> Health Bar Tutorial: Completed
  • Unity in Action (Pg 336 of 352)

Onward!

Advertisement

Day 45: Time.deltaTime

I grew up playing on dedicated consoles like the Sega Genesis where a game would run exactly the same no matter whose Sega you played it on.

On PC, developing a game means having to adapt code to run smoothly on virtually an unlimited amount of computer configurations.

This is where the concept of Time.deltaTime comes in. (I’m just reiterating what I’ve learned to solidify the points in my head – feel free to correct me if my understanding on any of the points are wrong)

Let’s say I’m creating a game where I want to move an object at the speed of 6:

public class moveObject : MonoBehaviour
{
     public float speed = 6f;
      
     void Update()
     {
         transform.Translate (speed, 0, 0)
     }

But I’m poor and have a slow computer. It runs the game at 30 frames per second.

FPS (30) * speed (6) = 180 (distance moved)

Someone else has a faster computer. It plays at 55 frames per second.

FPS (55) * speed (6) = 330 (distance moved)

In one second, the game object on the faster computer traveled nearly twice as far in the same time because it called the Update() function 25 more times in one second.

Time.deltaTime is used make this movement frame independent. It represents the time passed in seconds since the last Update().

public class moveObject : MonoBehaviour
{
     public float speed = 6f;
      
     void Update()
     {
         transform.Translate (speed * Time.deltaTime, 0, 0)
     }

My Slow Computer
30 (frames) / 1 (second) = 0.0333333333333333 (time between updates)
Speed * Time.deltaTime =
6 * 0.0333333333333333 = 0.2 (amount moved each frame)
30 FPS * 0.2 = 6 (distance moved each second)

Someone’s Faster Computer
55 (frames) / 1 (second) = 0.0181818181818182
Speed * Time.deltaTime =
6 * 0.0181818181818182 = 0.109090909909091
55 FPS * 0.090909099090909 = 6 (distance moved each second)

Using Time.deltaTime made the distance traveled by the object frame independent. Now no matter how fast or slow your computer runs, it will move at the same rate on screen as another player’s.

The same principle applies on the same computer. If I have a lot of programs running, my game might be running slower at some points and faster at others. Time.deltaTime will smooth the movement of the object instead of it looking twitchy each time Update() is called and the object ‘rushes’ to catch up.

In the same vein of frame rate independency, I also need to learn about FixedUpdate() for physics calculations.

Current Progress Status

  • Tower Defense Tutorial: 47/73
  • Unity in Action (Pg 324 of 352)

Onward!

%d bloggers like this: