2D Isometric Diagonal Movement Angles

Hi Everyone,

I’m very new to writing code but I’ve managed to piece together a movement script for my 2D isometric game. The problem is, I cant seem to fix the angles when moving in any diagonal direction.

Currently the player will move along the diagonal direction but never along the lines of the isometric grid I’m using in the tilemap. In the first image we can see the start position of the player on the pink line of tiles. If I move up and to the left he will end up on the green line as he reaches the end (Picture 2). The script is below.

Any help will be appreciated.

void Update()
    {
        PlayerInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized;

        // Smooth the input direction
        smoothedMovementDirection = Vector2.Lerp(smoothedMovementDirection, PlayerInput, smoothingFactor);

        // Update last non-zero movement direction only when the player is moving
        if (PlayerInput != Vector2.zero)
        {
            lastNonZeroMovementDirection = smoothedMovementDirection;

            // Update facing direction based on last non-zero movement direction
            if (lastNonZeroMovementDirection.x < 0)
            {
                facingLeft = true;
            }
            else if (lastNonZeroMovementDirection.x > 0)
            {
                facingLeft = false;
            }
        }
    }

    public Vector2 GetLastMovementDirection()
    {
        return lastNonZeroMovementDirection;
    }

    void FixedUpdate()
    {
        Vector2 moveForce = PlayerInput * moveSpeed;
        moveForce += forceToApply;
        forceToApply /= forceDamping;

        if (Mathf.Abs(forceToApply.x) <= 0.01f && Mathf.Abs(forceToApply.y) <= 0.01f)
        {
            forceToApply = Vector2.zero;
        }

Hey could you edit your post to use code tags? Also it’s good question-asking practice to post the simplest code that still has the problem so we don’t see all the details that aren’t relevant to the core problem.

As for the problem (if I understand it right), if you want to force more isometric-like movement then you want to scale the y-component of the movement down. This is to move slower up-and-down since the character is traversing tilemap cells too quickly by default. For “true” isometric, the y-scalar is .57735 but this looks like traditional isometric and I don’t have that off the top of my head. You then normalize the vector2 again then multiply that by your speed. Adapt for your circumstance.

tldr: you probably want to move slower in the y-direction

1 Like

Hey Lo-renzo, I’ve edited the post. After some tinkering with the script I finally got my diagonal movement to work! Thank you for the help.