Search Unity

preset movements

Discussion in 'Scripting' started by GlitchInTheMatrix, Dec 21, 2014.

  1. GlitchInTheMatrix

    GlitchInTheMatrix

    Joined:
    Apr 12, 2010
    Posts:
    285
    Hi guys, Im looking for a good way to how set commands for a character control.

    I.E.
    down, forward and a button do something, or back, forward and a button

    any idea how to code it?

    thanks a lot
     
  2. MathiasDG

    MathiasDG

    Joined:
    Jul 1, 2014
    Posts:
    114
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Input.GetKey(KeyCode.W))
    4.         {
    5.             Vector3 position = transform.position;                                  //  transform current position
    6.             Vector3 forward = transform.forward;                                    //  3d direction the transform is looking at.
    7.             transform.position = position + forward * speed * Time.deltaTime;       //  moving forward
    8.         }
    9.         if (Input.GetKey(KeyCode.S))
    10.         {
    11.             Vector3 position = transform.position;                                  //  transform current position
    12.             Vector3 forward = transform.forward;                                    //  3d direction the transform is looking at.
    13.             transform.position = position - forward * speed * Time.deltaTime;       //  moving backward
    14.         }
    15.         if (Input.GetKey(KeyCode.D))
    16.         {
    17.             Vector3 position = transform.position;                                  //  transform current position
    18.             Vector3 forward = transform.forward;                                    //  3d direction the transform is looking at.
    19.             Vector3 right = new Vector3(forward.z, forward.y, -forward.x);          //  2d clockwise perpendicular vector to (x, 0, z)
    20.             transform.position = position + right * speed * Time.deltaTime;         //  moving to the right
    21.         }
    22.         if (Input.GetKey(KeyCode.A))
    23.         {
    24.             Vector3 position = transform.position;                                  //  transform current position
    25.             Vector3 forward = transform.forward;                                    //  3d direction the transform is looking at.
    26.             Vector3 right = new Vector3(forward.z, forward.y, -forward.x);          //  2d clockwise perpendicular vector to (x, 0, z)
    27.             transform.position = position - right * speed * Time.deltaTime;         //  moving to the left
    28.         }
    29.     }
    Some simple 2d movement on the xz plane.
    This should get you started