- Home /
Get Navagent to move upward off the navmesh
I'm making a game with some civilians controlled by navagents on a navmesh, and periodically I need them to be abducted by flying saucers and move off the Navmesh directly upward. However, when I disable the navagent component to move it up, the gameobject continues to move in the direction it was last moving when the navagent was disabled, so instead of moving directly upward to the flying saucer, it moves up diagonally away from it.
I tried adding the Stop() method before disabling the navmesh, but that gives me the error message "'Stop' can only be called on an active navagent that has been placed on a navmesh."
Here's a code snippet that seems to be the problem. Anyone have any ideas?
if (captured == true)
{
agent.SetDestination(flyingSaucer.transform.position);
agent.velocity = Vector3.zero;
agent.speed = 0;
agent.Stop();
agent.enabled = false;
this.GetComponent<Rigidbody>().useGravity = false;
transform.position = Vector3.MoveTowards(transform.position, flyingSaucer.transform.position, abductedSpeed * Time.deltaTime);
}
Is that in update, being called every frame? Or only when the trigger is first hit?
If it's being called every frame, then you are calling Stop() on an inactive Nav$$anonymous$$eshAgent.
@FortisVenaliter I was calling it on update, but I just fixed that and I'm still having the same trouble. Here's my update code: private void Update() { //normal movvement when citizen is not abducted. if (captured == false) {
if (target != null)
agent.SetDestination(targets[targetNum].transform.position);
if (agent.remainingDistance > agent.stoppingDistance)
{
character.$$anonymous$$ove(agent.desiredVelocity, false, false);
}
else
{
if (targetNum <= targets.Length - 1)
{
targetNum++;
agent.SetDestination(targets[targetNum].transform.position);
}
else
targetNum = 0;
}
}
//on trigger with flying saucer beam, navagent is turned off and this causes the citizen to rise upward
else if (captured == true)
{
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, flyingSaucer.transform.position, abductedSpeed * Time.deltaTime);
}
}
And here's the method that is called by the collider on the other object on collision (the citizen has a box collider and the other has a trigger collider, so I had to have this method called by the other object:
//this is called by abductscript on its ontriggerenter().
public void Abducted()
{
agent.SetDestination(flyingSaucer.transform.position);
agent.velocity = Vector3.zero;
agent.speed = 0;
agent.Stop();
agent.enabled = false;
this.GetComponent<Rigidbody>().useGravity = false;
captured = true;
}
Okay, that looks like it should work, generally. I would remove the SetDestination call in the Abducted() function, because it's redundant when you're disabling the component immediately afterward.
But what is the problem you're seeing now?
Answer by IgorAherne · Jan 31, 2017 at 11:57 PM
Use offMeshLink
Here are a few functions which you can use, in particular inspect "Variables" and "Public Functions"
Also, look into NavMeshAgent API, to be specific, CTRL+F ---> have a look at anything with "offMeshLink" inside of it