0
\$\begingroup\$

I am creating a 2D turn-based isometric strategy game in Unity.

In this game, the visible map is divided into logical tiles. A character occupies a single tile at a time, and a tile can host up to one character at a time. The player can navigate their character through this grid using a D-pad (Up, Down, Left Right). A character can move from tile-A to tile-B if:

  • Tile-B does not host a character
  • Tile-B is "traversable"
  • |tile-A.height - tile-b.height| <= 1

I followed this tutorial to set up my isometric game environment. I was able to get a basic movement script working, but this simply moves the sprite across the screen, it doesn't bind it to the logical grid.

I'm not asking for someone to just do the work for me, but can someone point me in the right direction? I've tried googling and haven't gotten useful results.

Here is my current movement script. It obviously doesn't implement grid logic, it just directly manipulates screen position. I just don't know where to start for switching to the movement schema described above.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(SpriteRenderer))]
public class Simple_Char_Move : MonoBehaviour
{
    [SerializeField]
    private float speed = 1;
    
    private SpriteRenderer characterSprite;

    // Start is called before the first frame update
    void Start()
    {
        characterSprite = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        MoveCharacter();

        FlipSpriteToMovement();
    }

    void MoveCharacter()
    {
        //I am putting these placeholder variables here, to make the logic behind the code easier to understand
        //we differentiate the movement speed between horizontal(x) and vertical(y) movement, since isometric uses "fake perspective"
        float horizontalMovement = Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime;
        //since we are using this with isometric visuals, the vertical movement needs to be slower
        //for some reason, 50% feels too slow, so we will be going with 75%
        float verticalMovement = Input.GetAxisRaw("Vertical") * speed * 0.5f * Time.deltaTime;


        this.transform.Translate(horizontalMovement, verticalMovement, 0);
    }

    //if the player moves left, flip the sprite, if he moves right, flip it back, stay if no input is made
    void FlipSpriteToMovement()
    {
        if(characterSprite != null )
        {
            if (Input.GetAxisRaw("Horizontal") < 0)
                characterSprite.flipX = true;
            else if (Input.GetAxisRaw("Horizontal") > 0)
                characterSprite.flipX = false;
        }
    }
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Can you show us the movement script you're using at present? Off-site links have a bad habit of rotting, so it's always best to keep all relevant code samples contained in your question itself. \$\endgroup\$ Commented Aug 21, 2022 at 16:55
  • \$\begingroup\$ Sure, I can. That video shows the entire setup process for my maps as well, which I assumed was relevant for any suggestions for implementing the grid movement logic. That video doesn't provide any information about how to implement movement. \$\endgroup\$ Commented Aug 21, 2022 at 19:32

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.