Recap: Drive the Cube
Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 13 of 85.
This recap puts the chapter together: a position set once in Start(), then moves and turns on every frame in Update(), scaled by Time.deltaTime and asked with Input.GetKey. A position is the object's center, and all code goes inside the methods' braces.
Challenge
EasyPut the chapter together: place, drive and spin a cube.
- In
Start(), place the 1 unit tall cube on the ground at x =-3, z =0. - In
Update(), while the right arrow is held, move it right atspeedunits a second. - While Space is held, spin it around y at
spindegrees a second.
Press Play and hold the arrow, then Space. Then Check.
Try it yourself
using UnityEngine;
public class Driver : MonoBehaviour
{
public float speed = 3f;
public float spin = 180f;
void Start()
{
}
void Update()
{
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Unity Scripting Basics
1C# Basics in Unity
Hello, UnityVariablesDecimals Need an fText in the ConsoleVariables That RememberIf and ElseCompare NumbersRecap: Coin Collector2Make It Move
Place the CubeMove Every FrameTurn Every SecondSteer with the KeysRecap: Drive the Cube5Vectors
Adding VectorsDirectionsLocal and World SpaceDiagonal SpeedDistance Between ThingsVector3 Is a StructRecap: Drone Pilot8GameObjects and Components
References in the InspectorGet a ComponentMissing ComponentsAdd a ComponentRequireComponentShow and HideRecap: Light Switch6Transforms and Hierarchy
Parents and ChildrenHoist the FlagScaleLook At ThingsMove Toward a PointRecap: Orbiting Moon9Project: Catch the Star
The CatcherA Falling StarPractice on your own: Unity playground