1
\$\begingroup\$

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!

\$\endgroup\$

3 Answers 3

3
\$\begingroup\$

The first sample code is what you want. http://unity3d.com/support/documentation/ScriptReference/Transform.Translate.html

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" http://unity3d.com/support/documentation/Components/class-InputManager.html

\$\endgroup\$
0
0
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Nov 7, 2011 at 12:21
0
\$\begingroup\$

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. :-)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .