Preamble:

Unity’s transform.Translate method involves modifying the x,y,z values of our sprite’s transform component. Because we increase or decrease our values in every frame, this gives an illusion of movement.

To do this first we drag the sprite to be moved on our scene:

Dragging the Sprite

Then we create a script to code its behaviour. In this case I’ll call it as TranslateMovement, but usually this script should carry your entity’s name. I, then, attach it to my Spaceship:

When I double click on this, Visual Studio will fire to edit this file. We’ll meddle with the Update method, because as I’ve said, this way of doing movement is an illusion; we switch position in every frame.

transform.Translate()

This is the most basic way of doing movement, and currently – because we haven’t introduced any key press checks it’ll only move upwards in an insane speed.

Transform.Translate() moves our game object in a direction(taken as a Vector3) and distance provided in the function.

Why?

Because Vector3.up means new Vector3(0,1,0); under the hood and if you remember Update function is running 60 times per second, this gives our spaceship 60 units of upward per second. Hence the blazing fast result!

Fixing the fastness

We can use a normalizing function to make our Translate function move one unit per frame. To do this we should use Time.deltaTime:

void Update () {
transform.Translate(Vector3.up * Time.deltaTime);
}

This will make our spaceship rise one unit per second. Now for the sake of completeness let’s introduce keypresses and the speed variable to make it more smooth:

Detecting Keypresses

I’m going to use Unity’s Input Manager to handle detecting keypresses. In our case when we press W and S (or up and down arrow) we want to move up and down. Luckily for us Unity comes out with those keys mapped to Vertical Axis. To check Input Manager:

To modify our movement function with this keypresses we should write this code:

	void Update () {
        float verticalInput = Input.GetAxis("Vertical");
        transform.Translate(Vector3.up * verticalInput *Time.deltaTime);
	}

Let’s explain this code;
verticalInput gets a value between -1 and 1, 0 meaning nothing is pressed.
I get this value and use in a multiplication calculation, as I’ve said Vector3.up means new Vector3(0,1,0) so when verticalInput returns a minus one it becomes new Vector3(0,-1,0); which means downward movement (negative move on Y axis).

So when I add a horizontalInput and write another Translate function to handle left and right, I’ll have a spaceship moving with my keypresses:

	void Update () {
        // Get Keypresses
        float verticalInput = Input.GetAxis("Vertical");
        float horizontalInput = Input.GetAxis("Horizontal");

        // Make movement
        transform.Translate(Vector3.up * verticalInput *Time.deltaTime);
        transform.Translate(Vector3.right * horizontalInput * Time.deltaTime);
	}

We can also introduce a speed variable to make it more faster and if need be we can produce powerups to modify this value. But that’s your choice.

This shows how to make basic movement with transform.Translate() method

Categories: Unity