Search Unity

NavMesh Agents can't carry Rigidboys withou jitter

Discussion in 'Navigation' started by LegacySystem, Apr 22, 2022.

  1. LegacySystem

    LegacySystem

    Joined:
    Jan 28, 2021
    Posts:
    52
    Hi everybody,

    On my game NavMesh Agentsa must carry som object like a bulldozer style. The problem is (i think) agents directly modifies transform.position .Becuse of that nearly always the carried object keeps tunneling through bulldozer. I tried Interpolation and contniuos for all objects but didn't fixed it. So I tried to disable updatePosition and tring to move with rigidbody itself using rb.velocity=navMeshAgent.velocity but I got the following image :) .Left circle is the NavmeshAgent's reported position, right circle is the transform.position the agent is attached to. I tried navMeshAgent.nextPosition = transform.position; but it caused agent to jitter. I can't think anything else. How can I make agents cable of carrying object without collision bug or any jitter ?

    Here is the final code that works for collision but causes jitter.

    Code (CSharp):
    1. public class Enemy : MonoBehaviour
    2. {
    3.     Rigidbody rb;
    4.     NavMeshAgent navMeshAgent;
    5.  
    6.     public Transform Target;
    7.  
    8.     private void Start()
    9.     {
    10.         navMeshAgent = GetComponent<NavMeshAgent>();
    11.         rb = GetComponent<Rigidbody>();
    12.         navMeshAgent.updatePosition = false;
    13.         navMeshAgent.Move(Target.position);
    14.     }
    15.  
    16.     private void Update()
    17.     {
    18.         if (navMeshAgent.enabled)
    19.         {
    20.             rb.velocity = navMeshAgent.velocity;
    21.             navMeshAgent.nextPosition = transform.position;
    22.         }
    23.  
    24.     }
    25. }
     

    Attached Files:

  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    You are using here 2 Separate Movement/Physics Systems to work together,
    they are not made (at least from my experience) to work like this together.

    Why dont you use Rigidbody API to move your Character, towards a certain destination?
    Why you dont use the NavMesh API to move to a certain destination?
     
  3. LegacySystem

    LegacySystem

    Joined:
    Jan 28, 2021
    Posts:
    52
    Hi,
    Thank you for your reply. The enemy is an AI which works as an shovel vehicle. It will sweep some items (cubes mostly) from one place to another using phsycs. We use navmesh for obstacle avoidance and path finding.

    - With kinematic rigidbody and navmesh for movement, most of the times cubes are just lag behinds the colliders or even if they dont, they (cubes) jitter.
    - With non kinematic velocity movement and updatePosition =false, weird things happening (like navmesh agent reported position and transform position detached from each other as can be seen on first image)
    - With non kinematic velocity movement,updatePosition =false and forcefully navMeshAgent.nextPosition = transform.position. This causes the agent it self to jitter
     
  4. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    In that case, if you dont want to create your own "Object avoidance system", you can take maybe the "easy route" and disable the Components based on your needs, otherwise i would recommend CharacterController, and create your own Physics (pushing objects on collision) and gravity, which both are very simple to do.

    Also obv. there are more ways but those seem for me the "simple ones" which i could recommend, if you are into more difficult options, let me know