Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Trying to write a basic script to attach to the player on Unity 3D, I want them to move forward automatically. I'm guessing that will be part of the Update function and using transform, but that's about as far as I get.

I'm assuming it's a Transform.translate function too, but not sure what parameters to use to move forward 1 m/s automatically (for example). Also, how do I block W and S keys moving forwards and backwards, and instead use the for up and down? (My character is 'floating' and I'm looking to incorporate space harrier style controls)

Thank you!

share|improve this question
up vote 3 down vote accepted

The first sample code is what you want.

function Update() 
    transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * Time.deltaTime);

Vector3.forward is the same as (0,0,1), and transform.Translate() is local translate, so Translate(0,0,1) is always moving forward. Time.deltaTime let speed equal to 1 m/s.

And you can change the default key in "Input Manager"

share|improve this answer
Ah, great, thank you :) – David Archer Nov 7 '11 at 12:32

You should be using a Character Controller and use either SimpleMove or Move.

share|improve this answer
Are there any tutorials, videos etc. that covers this is a little more detail? Just looking at those links has only succeeded in confusing me a little more. – David Archer Nov 7 '11 at 12:21

this is C# version

public float speed=1f;

void Update() 
    transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * Time.deltaTime*speed);

you will have a field in inspector to change speed when you add the script to a gameobject. :-)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.