Camera movement (17622)

could anyone write me a script which would allow my camera to move on the z and x axis when i press the W,A,S,D keys?

3 Answers

3
var speed = 10.0f;

function Update()
{
    var movement = Vector3.zero;

    if (Input.GetKey("w"))
        movement.z++;
    if (Input.GetKey("s"))
        movement.z--;
    if (Input.GetKey("a"))
        movement.x--;
    if (Input.GetKey("d"))
        movement.x++;

    transform.Translate(movement * speed * Time.deltaTime, Space.Self);
}

Alternatively:

var speed = 10.0f;

function Update()
{
    var movement = Vector3.zero;

    movement.z = Input.GetAxis("Vertical");
    movement.x = Input.GetAxis("Horizontal");

    transform.Translate(movement * speed * Time.deltaTime, Space.Self);
}

Not only that, I also tagged it write-my-script. And I usually do unless it's quite involved. This was so trivial it would been more an effort to get references. :)

would you be able to make the camera move on the z and x axis which ever way the camera may be pointing even if its pointing down?

Just change Space.Self to Space.World.

Check out http://www.unifycommunity.com/wiki/index.php?title=Scripts if you want something premade. Like http://www.unifycommunity.com/wiki/index.php?title=MouseLookPlus or http://www.unifycommunity.com/wiki/index.php?title=MouseLookPlus2

can you pur it in C# ?

I changed that to a semi colon, but now it says it was expecting a semi colon weird.
EDIT: Replied to the wrong post XD