0
votes

First of all... I am not a native English speaker, so sorry for the bad English skills, I am doing my best to improve them.

so, I was working on this simple Character movement system and when I was testing I realized that if a press down "W"or "S" keys the player will move forward,and if I press "A" or "D" the player will move backwards and not sideways, and I have been searching and searching and I couldn't find anything similar to my problem... can someone help me?

    void Update () {
    // input
    Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
    Vector2 inputDir = input.normalized;
    bool running = Input.GetKey (KeyCode.LeftShift);


    Move (input, running);

    if (Input.GetKeyDown (KeyCode.Space)) {
        Jump ();
    }
}

void Move(Vector2 inputDir, bool running) {

    float targetSpeed = ((running) ? runSpeed : walkSpeed);
    float realSpeed = targetSpeed * Time.deltaTime;


    Vector3 directionToMove = new Vector3 (inputDir.x, 0, inputDir.y) * realSpeed;

    transform.Translate (directionToMove);
}
1
What exactly is the problem? Pressing "W" will move it forward(+z). Pressing "S" will move it backwards(Z-). Right Movement = "D" key. Left movement = "A" key. Isn't that how "WASD " is supposed to work? Are you trying to do something else? - Programmer
When i press "S" he doesn't move backwards, he move forward too,and when i press "A" he move backwards, when i press "D" he also move backwards - Nícolas
Check the answer I left below - Programmer

1 Answers

2
votes

When i press "S" he doesn't move backwards, he move forward too,and when i press "A" he move backwards, when i press "D" he also move backwards

The code is fine and should work as expected.

It is likely that your Input settings has been modified by you or an external plugin. Sometimes, it's just a bug. Simply reset it and the "WASD" keys should work fine.

Go to Edit ---> Project Settings ---> Input then click on the settings icon and reset it.

enter image description here


If the problem is still there then make sure that you are not moving this object from another script. Try to disable other scripts to see which one is causing the issue.