Search Unity

How to move in different directions at the same time

Discussion in 'Scripting' started by WashyRules, Apr 19, 2015.

  1. WashyRules

    WashyRules

    Joined:
    Apr 12, 2015
    Posts:
    3
    Hello, i'm learning to use unity and i'm trying to make a simple scene in wich you move a block over a quad with a first person camera. I almost achieve it, but i don't know how to make the block move in different directions at the same time. Like if you press 'w' and 'a' it moves forward and left at the same time (diagonally).
    This is all the code: http://imgur.com/kJIQ9TC
    I would like to know if this is the correct way to do it too, if possible. Thanks.
     
  2. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    Use the code tags when posting code. http://forum.unity3d.com/threads/using-code-tags-properly.143875/
    And perhaps you could do something like this.
    Code (CSharp):
    1.         float x=0;
    2.         float z=0;
    3.         if (Input.GetKey(KeyCode.W))
    4.             x=1;
    5.         if (Input.GetKey(KeyCode.S))
    6.             x=-1;
    7.         if (Input.GetKey(KeyCode.A))
    8.             z=1;
    9.         if (Input.GetKey(KeyCode.D))
    10.             z=-1;
    11.         transform.rigidbody.velocity=transform.TransformDirection(new Vector3(x, 0, z));