- Home /
Question by
CCnockaert · Jul 20, 2019 at 03:25 PM ·
movementnavmeshnavmeshagent
Change agent type stops movement.
Hello,
I'm trying to change the agent type of a NavMesh Agent at runtime, the problem is that when it's done the destination is reset, so the character doesn't move.
I tried to store the old destination ( before changing the agent type ), to set it as the new one, but you can still notice that the character stops moving for some moments.
Is there any way to prevent the NavMesh Agent to reset destination when its agent type is getting changed ?
Here is my current code :
protected void ChangeElement(Element element)
{
// get the element surface the character currently moves on
ElementSurface walkedElementSurface = GetCurrentWalkedElementSurface();
// element can be changed
if (walkedElementSurface == null || walkedElementSurface.Element == element || walkedElementSurface.Element.IsWeakAgainst(element))
{
// take old agent destination
Vector3 oldDestination = agent.destination;
// set element agent type
agent.agentTypeID = AgentTypeID.GetAgenTypeIDByName(element.ElementName);
// set up old destination as the new one
agent.SetDestination(oldDestination);
this.element = element;
print(name + "'s element changed to " + element.ElementName);
}
// element can't be changed yet
else
{
print("Can't change the element of " + name + " from " + this.element + " to " + element + " yet.");
}
}
Comment