0
\$\begingroup\$

I'm using an isometric tilemap(2:1) and I can't figure out how to make grid based movement on it's surface. I managed to get a code working for the grid based movement, moving my character with 0.5 along the Y axis and 0.86 ( sqrt(3)/2 ) along the X axis since I imagine it should work as if they are diamonds with the side of 1 unit, but it doesn't align with the center of the next tile. Here is my movement code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions.Must;

public class Movement : MonoBehaviour
{
    Vector3 pos;         // For movement
    float speed = 5f;   // Speed of movement
    public float x = 0.86f;
    public float y = 0.5f;

    void Start()
    {
        pos = transform.position;          // Take the initial position
    }

    void FixedUpdate()
    { 
        if (Input.GetKey(KeyCode.A) && transform.position == pos)
        {        // Left
            //pos += Vector3.left;
            pos += new Vector3(-0.86f, 0.5f, 0);
        }
        if (Input.GetKey(KeyCode.D) && transform.position == pos)
        {        // Right
            //pos += Vector3.right;
            pos += new Vector3(0.86f, -0.5f, 0); // miscare in diagonala
        }
        if (Input.GetKey(KeyCode.W) && transform.position == pos)
        {        // Up
            //pos += Vector3.up;
            pos += new Vector3(0.86f, 0.5f, 0);
        }
        if (Input.GetKey(KeyCode.S) && transform.position == pos)
        {        // Down
            //pos += Vector3.down;
            pos += new Vector3(-0.86f, -0.5f, 0);
        }
        transform.position = Vector3.MoveTowards(transform.position, pos, Time.fixedDeltaTime * speed);    // Move there    
}   

The first frame, where the player is aligned on the grid After input the player moves 2 tiles, instead of one, and is not aligned anymore with the center of the tile Tilemap inspector view Grid inspector view

\$\endgroup\$
  • \$\begingroup\$ In the strict sense, 2:1 is dimetric, not isometric. \$\frac {\sqrt 3} 2\$ is correct for true isometric, but for 2:1 dimetric it's just 1. The whole reason games use the neat integer 2:1 ratio is so we don't have to deal with irrationals like \$\sqrt 3\$ \$\endgroup\$ – DMGregory May 11 at 13:44
  • \$\begingroup\$ Many thanks, using 1 instead of 0.86 solved the issue, almost, it still moves 2 tiles instead of 1 so I had to halve both the X and Y movement (move 0.5 instead of 1 along X axis and 0.25 along the Y axis), could you explain why it's like this instead of 1 and 0.5 since I can't seem to figure out the reason? \$\endgroup\$ – Mihai Barbu May 11 at 15:14
  • \$\begingroup\$ That sounds like it's down to the size of the tiles in your game world. Want to edit your question to show us how you've set up your map in the Scene view and Inspector, and we can walk you through how to convert that to movement intervals? \$\endgroup\$ – DMGregory May 11 at 15:22
  • \$\begingroup\$ The grid cell size is 1, 0.5, 1 and the tilemap is scaled at 1,1,1 , so, why moving by 1 gets me twice as far? \$\endgroup\$ – Mihai Barbu May 11 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.