vote buttons
22 Feb 2015 by K Bonneau

Beginning Unity 3D Scripting for Developers [Part 2]


Adding Some Movement

All game objects have a Transform component which can be accessed via GameObject's transform property which is of type Transform. The Transform class has a property called position which indicates the position of the game object in the scene. It is of type Vector3. So if we wanted to change the position of the sphere to say x=2, y=2, z=2 we can do it as below: 

sphere.transform.position = new Vector3(2,2,2);

As we know that the Update() function gets called every frame. So on every call to update, if we add a small value to the x component of the position, we will get a movement in X direction.

Suppose we want to increment the x value of position by 0.01 each frame, we can achieve this by adding the following statement in the Update() function.

sphere.transform.position += new Vector3(0.01f, 0, 0);

The Vector3 has already some predefined Vectors which we can use:

Vector3.left = new Vector3(1f,0,0);
Vector3.right = new Vector3(-1f,0,0);
Vector3.forward = new Vector3(0,0,1f);
Vector3.back = new Vector3(0,0,-1f);
Vector3.top = new Vector3(0,1,0);
Vector3.bottom  = new Vector3(0,-1,0);

So if we want to move left by .01 each frame we can say

sphere.transform.position += Vector3.left * .01f;

There is another way to achieve the same result using Transform's Translate function:

sphere.transform.Translate (Vector3.left * .01f);

Add the above line to your Update function. The code should look like below:

using UnityEngine;
using System.Collections;

public class Main : MonoBehaviour {
	GameObject sphere;
	// Use this for initialization
	void Start () {
		sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
		sphere.renderer.material.color = Color.red;
	// Update is called once per frame
	void Update () {
		sphere.transform.Translate (Vector3.left * .01f);

Build the script (F8). Go back to Unity UI, and hit play to see the ball moving.

This should move your sphere quite well, but one thing that you should understand is that frame rate is not constant. For a Complex scene this becomes much more of an issue. The Update function gets called for each frame and we move by .01 during that period. The problem is the speed of the sphere depends on the frame rate. If frame rate is 20, the Update gets called 20 times in a second, so the sphere moves 20 * .01 = 0.2 units per second. If you run it on a faster computer at 60 frames per second, the sphere will move at a speed of 60 * .01 = 0.6 units per second. If we want the speed of the sphere to be independent of frame rate, we should check how much time has been spent since last frame, and move the sphere in proportion to that. i.e if we want speed to be constant 1 unit per second, and if it has taken 2 seconds since last frame (Exaggeration) we should move by  2 units (so that 2 units in 2 secs = 1 unit per sec). If only one second has passed we move by 1 unit.

So to improve the above update function we need to know the time between frames. Fortunately we can access the time it took to complete the last frame using Time.deltaTime (in seconds). We'll update our Update function to use this as shown below.

void Update () {
	sphere.transform.Translate (Vector3.left * Time.deltaTime);

If you want to double the speed, you can say Vector3.left * Time.deltaTime * 2 and be sure that the speed will remain the same irrespective of frame rate.