- Home /
Question by
m4thus4n · Jun 23, 2021 at 05:34 AM ·
2dtilemappathfindingisometric
Move towards move very fast independent of the speed
I am working on a tile based isometric game. My character uses A* pathfinding to find his way. So, I need to move the player after the given path. I use the function MoveTowards. But the player moves very fast even with very low speed. (The speed doesn't seem to influence as with high speed, it doesn't seem to accelerate).
Here is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class GameManager : MonoBehaviour
{
public Pathfinder pathfinder;
public Tilemap tilemap;
public int startX;
public int startY;
public int goalX;
public int goalY;
private Vector3 start;
private Vector3 goal;
private Vector3Int tempTarget;
private int idx = 0;
public List<Node> path;
void Start()
{
start = tilemap.CellToWorld(new Vector3Int(startX, startY, 0));
transform.position = start;
pathfinder.Init(startX, startY, goalX, goalY);
path = pathfinder.GeneratePath();
goal = tilemap.CellToWorld(new Vector3Int(goalX, goalY, 0));
}
void Update() {
if (Vector3.Distance(transform.position, goal) > 0.1 && idx < path.Count) {
tempTarget = new Vector3Int(path[idx].PositionX, path[idx].PositionY, 0);
Vector3 pos = tilemap.CellToWorld(tempTarget);
while (Vector3.Distance(transform.position, pos) > 0.1) {
transform.position = Vector3.MoveTowards(transform.position, pos, 1f * Time.deltaTime);
}
if (Vector3.Distance(transform.position, tempTarget) > 0.1) {
idx++;
}
}
}
}
Comment
Answer by dev_mp · Jun 23, 2021 at 07:00 AM
You are making it move continuously for each segment on a while loop, so it will keep moving until distance to the next path node is =< 0.1.