In my script for a 2d Isometric movement, when pressing the right or left key my character moves up or down respectively. (Similar behavior with up and down keys.)

This is my first post so apologies ahead of time if I post to the wrong place or I am not clear enough. I am creating a character controller for a 2D Isometric game using the Y as Z tile map. When I compiled the script and ran the game to test it I noticed that: pressing right would move me up, pressing left would move me down, pressing down moves me left, and pressing up moves me right. I followed a lot of this tutorial for initial set up of my tilemap and used quite a bit of their character controller.(Learn Isometric 2D environments with Tilemap | Unity Blog) The main difference is I normalize my direction instead of clamping the magnitude to 1, and I multiplied direction and speed and then multiplied by delta time all in the same step.

Here is my script I hope someone can help.

[SerializeField]
private float CharacterSpeed = 1.0f;
private Vector2 InputDir;

public Rigidbody2D PlayerPhysics;

// collect references
void Awake()
{

    PlayerPhysics = GetComponent<Rigidbody2D>();

}

void Update()
{

    float VertInput = Input.GetAxis("Vertical");
    float HorInput = Input.GetAxis("Horizontal");
    InputDir = new Vector2(VertInput, HorInput).normalized;

}

void FixedUpdate()
{

    Vector2 WhereAmI = PlayerPhysics.position;
    Vector2 WhereTo = WhereAmI + (InputDir * CharacterSpeed) * Time.fixedDeltaTime;
    PlayerPhysics.MovePosition(WhereTo);

}

1 Answer

1

When making the InputDir Vector2 variable I had put vertical and horizontal floats in the wrong order making my input axis act the way they did. The 3rd line in Update should read:

 InputDir = new Vector2(HorInput, VertInput).normalized;