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-Bdoes not host a characterTile-Bis "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;
}
}
}