how to make a NavMeshAgent and an active ragdoll work together

so I have an active rag doll system I took from GitHub which was made by a youtuber but im having no luck in figuring out a solution, origanly I was going to have an empty object that used its own NavMeshAgent agent and the active rag doll would try and follow it but I quickly ran into a problem if the NavMeshAgent goes behind it will lose track of it and also it looks awful so im wondering how can I apply a NavMeshAgent agent to the active rag doll itself because in its current state I just makes a beeline for its target

no a navmesh agent is not going to play ball well with an active ragdoll at all - you will need a custom pathfinding solution

1 Like

Not really. It is possible to use navmeshagent without letting it directly control underlying object.

NavMeshAgent has “updatePosition” field. Set it to false. Once you do so, the navmesh agent will no longer drive the object directly.

So. Once the automatic sync of transformation is of, you can set and get pathfinding information from navmesh agent using “nextPosition” property.
https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent-nextPosition.html
https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent-updatePosition.html

So, in update, assign current position of your controlled physical object to “nextPosition” of NavMeshAgent.
And next frame compare simulated position of navmesh position to the position of your object. Compute a difference. That will tell you where your object wants to go. Based on that vector apply forces to your rigidbody.

This won’t be perfect, because the agent is unaware of properties of rigidbody, but it will work.

If you haven’t done so, I’d also recommend to take a look at “boids” algorithm and attempt to implement it. The concept of “direction where it wants to go” is very powerful.

2 Likes

Sure, my point was you cant just slap a navmesh agent on an active ragdoll (which is made up of multiple rigidbodies etc) and have it work

But seeing as the OP is relatively new to unity I would count that level of work as a “custom solution” as its not just out of the box as most would expect navmesh agents to work

But basically, we agree :smile:

You can actually do this without a navmesh agent entirely (which is what I also mentioned before in another post somewhere) where you can compute the navmesh path and then from the path get the corners as a list of vectors and use that to drive your object to the destination using whatever movement you like, so yeah theres lots of approaches - but none that just work without a fair bit of fiddling, and most will look not “perfect” with an active ragdoll

If op wants to look into this approach start with reading up and experimenting with https://docs.unity3d.com/ScriptReference/AI.NavMesh.CalculatePath.html

1 Like

thanks for the responses but yes I did know that the NavMeshAgent wouldn’t just work

well ill try and do this but I just want to know also how would I

Through programming in C#.

1 Like

lol that’s a funny response but really what’s the code sorry im awful at this part of coding =(

1 Like

wait where is the update position field is it in code if not please send a screen shot

I’ve already explained what you need to do. Read position, compare to real one, calculate the vector.
If you cannot do that by yourself, you need to improve your programming skills.

Implement “boids” in C#, like I recommended earlier. That will be a good t raining excercise.Or “game of life”. Or “frogger”.
https://en.wikipedia.org/wiki/Boids
https://en.wikipedia.org/wiki/Conway’s_Game_of_Life

People do not like answering “what’s the code” type of questions, because this sort of question implies that the asker doesn’t want to learn anything and instead want someone else to solve their problem for free.

Basically, imagine if someone at school asks you for help with homework. Will you teach them a little? Probably. What if they ask you to DO their homework for them? It is a similar thing.

People like to help with, but not do tasks for you.

2 Likes

It is in the code and is only available thorugh the code. You can’t see it in inspector. You’d need to Get the NavAgent component, and then you’ll be able to assign values to it.

1 Like

ye sry I already know this its just I really struggle with certain aspects of coding thanks for the response though

yay thanks also is the calculation of the offset required or is it just to make it a bit more accurate because if so its not going to do much when I said active rag doll I meant REALLY PHYSICS CONTROLLED

ohhhh ive just realised you can create a vector 3 at the position of the calculated position from the NavMeshAgent and have that set as its current target and do this every frame am I right =)

NavMeshAgent tells you where the object should be if it were following the path.
And your ragdoll is not going to be at that spot, because navmesh can’t drive it directly.
So given the position where the ragdoll SHOULD be, and where it IS, how do you determine where you should push it?

You should test it in code editor, instead of asking for ocnfirmation. Because there’s no reason to ask when you can test if it works, plus you’ll learn more while getting it to work.

There’s a reason why I recommended to try to implement Boids, by the way. You’d learn plenty of stuff if you manage to do that.

1 Like

ye I understand now thanks for all your help =)

wait can have a vector3 set that as the current next position of the NavMeshAgent and have that set as the target of the active rag doll

im having a slight problem when I say small I mean very big so when ever walkpointset = false it teleports the active rag doll also even though the NavMeshAgent agent shouldn’t be able to it moves as I can see it in the scene view whilst the game is running ill give you the code if you know what the problem is please show me what ive done wrong I’m really hating working with NavMeshAgent’s =(

public NavMeshAgent AI;
    public LayerMask whatisground;
    public Vector3 walkpoint;
    public bool walkpointset;
    public float walkpointrange;
    public Vector3 targetpoint;
    public Transform targettransform;

    private void Awake()
    {
        AI = GetComponent<NavMeshAgent>();
        AI.updatePosition = false;
    }

    // Update is called once per frame
    void Update()
    {
        targetpoint = GetComponent<NavMeshAgent>().nextPosition;
        patroling();
        targettransform.position = targetpoint;
    }

    void patroling()
    {
        if (!walkpointset)
        {
            searchwalkpoint();
        }

        if (walkpointset)
        {
            AI.SetDestination(walkpoint);
        }

        Vector3 distancetowalkpoint = transform.position - walkpoint;

        //walkpoint reached
        if (distancetowalkpoint.magnitude < 2.5f)
        {
            walkpointset = false;
        }
    }

    void searchwalkpoint()
    {
        //calculate random point in range
        float randomz = Random.Range(-walkpointrange, walkpointrange);
        float randomx = Random.Range(-walkpointrange, walkpointrange);
        walkpoint = new Vector3(transform.position.x + randomx, transform.position.y, transform.position.z + randomz);
        if (Physics.Raycast(walkpoint, -transform.up, 2f, whatisground))
        {
            walkpointset = true;
        }
    }

Because that’s exactly what you’re telling it to do

What do you think this fragment does?

    void Update()
    {
        targetpoint = GetComponent<NavMeshAgent>().nextPosition;
        patroling();
        targettransform.position = targetpoint;
    }

It teleports the target to position of the navmesh agent, every frame.

Do not teleport, do not assign positions, add forces to ragdoll instead.

As a simpler exercise, make a script where you have a flat plane, a GameObject that is target, and a ragdoll that is rolling ball. Then make the ragdoll reach the target without ever assigning values to its “position” field. For extra fun, make the target randomly teleport, and let the ball chase it. Once you get this working, you can repeat the same thing with pathfinding.

@EvilBob99

For a start, try to replicate this:

  • Red ball teleports,
  • white ball chases the red ball.
  • White ball is a ragdoll sphere that rolls around.
  • Flat plane
  • no pathfinding.
  • You’re not allowed to assign anything to transform.position of the white ball.

Once you do that, you’ll be able to deal with navmeshagent.

Did you cover newton’s laws, force, accelerated movement and concept of “vector” in school yet?