Basic WASD movement problem

Hi guys,
first: I’m VERY noob right now and english is not my native language. So please forgive me any mistakes.
I’m trying to make a simple script for WASD movement to attach to an object. It is actually working pretty well, but when I move left or right the object will slightly move upwards. I have no idea why. Please help!
Thank you!

using UnityEngine;
using System.Collections;

public class MoveWASD : MonoBehaviour
{
public float moveSpeed = 10f;
private float angleTo = 0;

void Start () {
Debug.Log (“I am alive! WASD”);
}

void Update ()
{
if (Input.GetKey (KeyCode.W)) {
transform.Translate (Vector2.up * moveSpeed * Time.deltaTime);
angleTo = 180f;
}

if(Input.GetKey(KeyCode.S)) {
transform.Translate(Vector2.down * moveSpeed * Time.deltaTime);
angleTo = 0f;
}
if(Input.GetKey(KeyCode.A)) {
transform.Translate(Vector2.left * moveSpeed * Time.deltaTime);
angleTo = 270f;
}

if(Input.GetKey(KeyCode.D)) {
transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
angleTo = 90f;
}

transform.rotation = Quaternion.Euler(0,0,angleTo);

}
}

Okay, well welcome new fella :slight_smile:

You were setting the angle, essentially on the first move in whatever direction (it was actually set/reset on every update, but for the purpose of your problem, let’s say its change was on the first new direction)

with all of the angle changes, I got the code working like this:

using UnityEngine;
public class MoveWASD : MonoBehaviour
{
    public float moveSpeed = 10f;

    void Start()
    {
        Debug.Log("I am alive! WASD");
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            transform.rotation = Quaternion.Euler(0, 0, 180);
            transform.Translate(Vector2.down * moveSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.S))
        {
            transform.rotation = Quaternion.Euler(0, 0, 0);
            transform.Translate(Vector2.down * moveSpeed * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.A))
        {
            transform.rotation = Quaternion.Euler(0, 0, 270);
            transform.Translate(Vector2.down * moveSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.D))
        {
            transform.rotation = Quaternion.Euler(0, 0, 90);
            transform.Translate(Vector2.down * moveSpeed * Time.deltaTime);
        }
    }
}

Another option was this:

void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            transform.rotation = Quaternion.Euler(0, 0, 180);
            transform.Translate(transform.up * moveSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.S))
        {
            transform.rotation = Quaternion.Euler(0, 0, 0);
            transform.Translate(-transform.up * moveSpeed * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.A))
        {
            transform.rotation = Quaternion.Euler(0, 0, 270);
            transform.Translate(transform.right * moveSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.D))
        {
            transform.rotation = Quaternion.Euler(0, 0, 90);
            transform.Translate(-transform.right * moveSpeed * Time.deltaTime);
        }
    }

Vector2/3.up vs transform.up : Difference between Vector.up and transform.up - Questions & Answers - Unity Discussions

There is pinned thread that explains how to post code properly on the forums :slight_smile:
Because you had rotated the object, it’s left/right/down/up were all shifted…

1 Like