- Home /
I am unable to update MoveTowards target during runtime. Object move only to first target.
I am currently working on a small game, but I'm having a problem with MoveTowards. I am trying to move this object towards the vector3 TargetPosition. This works with the initial value that I give.
When the move function is called, I want to update TargetPosition. However, the variable does not seem to update and my objects won't move. I have tested the values of the destination tile, and it seems fine?
Here is the relevant code:
public Vector3 TargetPosition;
public void setTile(Tile tile){
this.tile = tile;
}
void Start()
{
TargetPosition = tile.transform.position;
}
void Update()
{
//move
if(transform.position != TargetPosition){
float step = moveSpeed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, TargetPosition, step);
}
}
public void move(List<Tile> path, Tile destination){
TargetPosition = destination.transform.position;
}
Thanks for your help!
Comment