Teleporting nav mesh agent in stead of moving
I've already resolved my problem but I don't understand why it is now working and wasn't before...
I have this piece of code inside a function to set the destination of my agent. It is called every time the agent comes within 2f of its destination. It was working fine like this:
//wanderingRange is just how far the agent moves from his original spawnposition
int randomX = Random.Range((int)-wanderingRange, (int)wanderingRange);
int randomZ = Random.Range((int)-wanderingRange, (int)wanderingRange);
Vector3 newDestination = new Vector3(randomX,
transform.position.y,
randomZ);
agent.SetDestination(spawnPosition + newDestination);
currentDestination.position.Set(newDestination.x,
newDestination.y,
newDestination.z);
Then I changed it to this, thinking that it was shorter:
Vector3 newDestination = new Vector3(
Random.Range((int)-wanderingRange, (int)wanderingRange),
transform.position.y,
Random.Range((int)-wanderingRange, (int)wanderingRange));
currentDestination.position = spawnPosition + newDestination;
agent.SetDestination(currentDestination.position);
Now in this second piece of code, the agent all of a sudden started teleporting in stead of moving and basically was in a completely different location each frame and calculated a new destination every frame. I have no clue how this is possible but it is the only change I made in the code and after reverting it back to the 1st snippet, it started working normally again.
So my real question here is; how is one piece of code working and the other one isn't?