0

I started making a D&D-style turn based tactic game. I have a problem with my tilemap.

I made a script to find the location on the tilemap for my character and for the tile I clicked with the mouse. (Both are working: it shows me locations like (3,-2) etc.)

But when I try to move my character from location to target... My character is stuck, always in one place ~(0,0) in the gap between cells. Here is the code.


using UnityEngine;
using UnityEngine.Tilemaps;
public class CharacterMovement : MonoBehaviour
{
    public Tilemap tilemap;
    public float moveSpeed = 50f;
    public bool hasMoved = false;
    private Vector2Int targetPosition;
    private Vector2Int characterLocation;
private void Update()
{
    MoveToClickedTile();
}

private void MoveToClickedTile()
{
    // Check for left mouse button click
    if (Input.GetMouseButtonDown(0) && !hasMoved)
    {
        GetMouseLeftClickPosition();
        GetCharacterTileLocation();
        // works
        print(targetPosition + "target");   
        print(characterLocation + "character");

        // doesnt work
        transform.position = Vector2.MoveTowards(characterLocation, targetPosition, moveSpeed * Time.deltaTime);

        hasMoved = true;
    }
}

private void GetMouseLeftClickPosition()
{
    Vector2 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    targetPosition = (Vector2Int)tilemap.WorldToCell(mouseWorldPos);
}

private void GetCharacterTileLocation()
{
    characterLocation = (Vector2Int)tilemap.WorldToCell(transform.position);
}

}

Editor screenshot

Is the problem that my character does not "belong" to the tilemap? I made it as a separate game object, and its transform location is really strange, I don't understand why.

But when I made the character separate from the tilemap, it had a weird box that was much more bigger than the character, and I could not click on it to find out what it was.

DMGregory
  • 134,153
  • 22
  • 242
  • 357
James
  • 1
  • 1
  • Vector2.MoveTowards is moving only one step and your move is setting hasMoved right away on the first step to true. I'm surprised it even manages to go that far. You should call it in update and only terminate the condition once it reaches the destination, not right away or when your mouse is not down anymore. – Zibelas Sep 08 '23 at 15:22
  • It looks to me like your problem is the same one as described in this past Q&A: Why is Vector2.MoveTowards only working on a single frame? You're treating it like a tweening function that will continue executing the movement frame after frame until it arrives, but that's not what it does. (Vector2.OneStepTowards might be a clearer name for it) Are you able to solve your problem using the advice at the earlier Q&A link? – DMGregory Sep 08 '23 at 17:01
  • I've put this question on hold temporarily as a potential duplicate of the one linked above. If you find you need any help applying the guidance at that link to your use case, please edit your question to show how you've tried to apply it and where you've gotten stuck or what part you need help with, then we can re-open the question to get answers about that specific aspect. – DMGregory Sep 09 '23 at 14:21
  • Sorry that I didn't reply, I have visited my parents for the weekend, so I will be able to test your tips out tomorrow evening. Thanks and sorry :D – James Sep 09 '23 at 17:09

0 Answers0