Problem is solved
MoveTowards resets position
I want to move an object from his current position 1 unit along the y-axis like this:
StartCoroutine(move(transform.position));
IEnumerator move(Vector3 startPos)
{
transform.position = Vector3.MoveTowards(startPos, startPos + Vector3.up, walkSpeed);
yield return new WaitForSeconds(3f);
}
But when the function is recalled the object get's teleported back to the initial position and doesn't use it's new position as startPos for the movement. Also when I do...
Vector3 endPos = transform.position;
Debug.Log(startPos+ ":" + (endPos));
...Unity tells me, that the startPos is the same as the endPos after the movement, even tough the object clearly moved. Please help me, I've been struggling with this since 4 hours
Answer by IINovaII · Jan 04, 2020 at 03:23 PM
The line transform.position = Vector3.MoveTowards(startPos, startPos + Vector3.up, walkSpeed);
gets called only 1 time in that setup. If you want that code to repeat itself every 3 seconds, you have to wrap it around a loop. Even after that you'll only notice it move one time because the startPos doesn't change after reached the destination. So, you'll have to replace startPos with transform.position since it updates its position upon movement. In the end, the code would look something like this.
IEnumerator move()
{
while (true)
{
transform.position = Vector3.MoveTowards(transform.position, transform.position + Vector3.up, walkSpeed);
yield return new WaitForSeconds(3f);
}
}
You can replace the condition in the while loop in however way you see fit in case you don't want the object to move up indefinitely.
-Nova
It still doesn't work. As you can see in the gif, the object always returns to the first position when moveTowards gets recalled. Also, the object goes in the wrong direction, even though I wrote Vector3.up
Hello, can you share the entire script containing the move function? I might get a better idea of where the problem lies. -Nova
So basicly I want to move the object along the tiles I created, and so that it always finds the right way. In the Update() function I check if the object is moving and when is$$anonymous$$oving returns false, the object should move to the next tile, but ins$$anonymous$$d it goes the same path again.
void Update()
{
if (is$$anonymous$$oving == false)
{
StartCoroutine(move());
}
}
IEnumerator move()
{
is$$anonymous$$oving = true;
Vector3Int upperTile = Vector3Int.FloorToInt(transform.position) + Vector3Int.FloorToInt(Vector2.up);
Vector3Int rightTile = Vector3Int.FloorToInt(transform.position) + Vector3Int.FloorToInt(Vector2.right);
Vector3Int lowerTile = Vector3Int.FloorToInt(transform.position) + Vector3Int.FloorToInt(Vector2.down);
Vector3Int leftTile = Vector3Int.FloorToInt(transform.position) + Vector3Int.FloorToInt(Vector2.left);
if (tilemap.HasTile(upperTile) && tilemap.GetTile(upperTile).name == "Sidewalk")
{
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, transform.position + Vector3.up, walkSpeed);
}
else if (tilemap.HasTile(rightTile) && tilemap.GetTile(rightTile).name == "Sidewalk")
{
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, transform.position + Vector3.right, walkSpeed);
}
else if (tilemap.HasTile(leftTile) && tilemap.GetTile(leftTile).name == "Sidewalk")
{
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, transform.position + Vector3.left, walkSpeed);
}
else if (tilemap.HasTile(lowerTile) && tilemap.GetTile(lowerTile).name == "Sidewalk")
{
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, transform.position + Vector3.down, walkSpeed);
}
yield return new WaitForSeconds(3f);
is$$anonymous$$oving = false;
}