Search Unity

Help with a Physics movement-based NavMeshAgent

Discussion in 'Navigation' started by dann96, Sep 7, 2016.

  1. dann96

    dann96

    Joined:
    Dec 6, 2014
    Posts:
    113
    Basically, What I have is a gameObject that is moved by Rigidbody.addforce with the Agent.desiredVelocity as the direction input. The currently layout I have for the AI is that, if the gameObject position is a certain distance away from the Agent's simulated position (a distance of about 0.5f), the Agent will change its destination to the GameObject's positon in an attempt to reconnect with the GameObject.

    Is there a way for, instead of it changing destination and navigating its way back to the GameObject, I can just instantly reset the simulated position to the GameObject's transform.position? (keep in mind that while all of this is running, I am running Agent.nextPositon = transform.position in the update method, with the simulated postion only changing if their is a valid path to the GameObject's transform.position. in addition, if the Agent.nextPosition does manage to reconnect to the transform.position, the Addforce movement acts strangely upon reconnection, sliding about and not correctly following the desired velocity).
    (Also keep in mind that I need the Agent to rotate as well, whether it be through either RotateTowards or Agent.UpdateRotation = true).
     
  2. Jim_West

    Jim_West

    Joined:
    Aug 5, 2013
    Posts:
    49
    What I'm doing for my Rigidbody AI (basically forces the navmesh simulation to accept my next position):

    Code (CSharp):
    1. // use the values to move the character
    2. _pawn.Move(_agent.desiredVelocity.normalized);
    3. _agent.nextPosition = transform.position;
    4.  
    5.  
    6. // warp the agent if needed (sometimes the agent thinks its on a highter level, but the character is still on ground)
    7. if (Mathf.Abs(_agent.nextPosition.y - transform.position.y) > 0.5f )
    8. {
    9.     _agent.Warp(transform.position);
    10.     _agent.SetDestination(_target.position);
    11. }