Menu
CoddyTech

Move Every Frame

Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 10 of 85.

Unity calls Update() once for every frame it draws. Change the position a little in it and the object moves a little on every frame, which on screen looks like smooth motion.

+= on a position adds a Vector3 to it: each of x, y and z grows by the matching number. This balloon rises 0.02 units on every frame and stays where it is on x and z:

void Update()
{
    transform.position += new Vector3(0, 0.02f, 0);
}

The f is the C# float suffix you already know: Unity works in float, and 0.02 on its own would be a double. At 60 frames a second the balloon rises 0.02 x 60 = 1.2 units every second.

challenge icon

Challenge

Easy

Make the red car drive forward.

  1. In Update(), add new Vector3(0, 0, 0.05f) to transform.position with +=.
  2. Keep x and y at 0: the car moves only along z.

Press Play: the car drives away from the camera. Then Check.

Try it yourself

using UnityEngine;

public class CarMover : MonoBehaviour
{
    void Update()
    {
        // Move the car forward along z here
    }
}

All lessons in Unity Scripting Basics

Practice on your own: Unity playground