Move Every Frame
Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 3 of 67.
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
EasyMake the red car drive forward: in Update(), move it 0.05 units along the z axis on every frame. It should not move on x or y.
Press Play to watch it drive, then Check.
Try it yourself
using UnityEngine;
public class CarMover : MonoBehaviour
{
void Update()
{
// Move the car forward along z here
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Unity Scripting Basics
1Your First Script
Hello, UnityPlace the CubeMove Every FrameTurn Every SecondSteer with the KeysRecap: Drive the Cube4Vectors
Adding VectorsDirectionsLocal and World SpaceDiagonal SpeedDistance Between ThingsVector3 Is a StructRecap: Drone Pilot7Project: Catch the Star
The CatcherA Falling Star5Transforms and Hierarchy
Parents and ChildrenScaleLook At ThingsMove Toward a PointRecap: Orbiting Moon