Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Working with a navmesh and rigidbody

Discussion in 'Physics' started by BigBWolf, Aug 9, 2023.

  1. BigBWolf

    BigBWolf

    Joined:
    Jul 6, 2020
    Posts:
    2
    So I've been trying at this for a while now but I always fix an issue then another pops up. With each fix my code gets messier and messier. Basically, I'm creating a slime enemy in 3D that uses a navmesh agent to calculate a path but then I want to use rigidbody.addforce to jump in the direction it's facing. The hard part is disabling the navmesh, enabling the rigidbody, adding the force, wait for the force to be applied before renabling the navmesh before it repeats.

    My code is basically this:

    Code (CSharp):
    1. Update() {
    2.     if (isGrounded) {
    3.          //Turn off navmesh
    4.          //Turn on Rigidbody
    5.          //Add force
    6.          //Turn off rigidbody
    7.          //Turn on navmesh
    8.          //coroutine to space out
    9.          //jumps
    The problem I'm running into is it is reactivating the navmesh before all the force has been applied. I thought maybe another coroutine but that seems sloppy and not very great. Suggestions?
     
    Last edited: Aug 9, 2023
  2. maomaothegreat1

    maomaothegreat1

    Joined:
    Jul 6, 2023
    Posts:
    14
    you could try using a toggle were you
    if (isGrounded)
    {
    //Turn off navmesh
    //Turn on Rigidbody
    //Add force "an impulse force"
    }
    if you are using a ground check like a raycast or physics overlap the isGrounded bool would then go false
    then the next time you hit the ground the bool is set true
    so something like
    if (isGrounded==false)
    {
    if (//your ground check function so a raycast or a physics overlap)
    {
    //Turn off rigidbody
    //Turn on navmesh
    //coroutine to space out
    isGrounded=true;
    }
    }
    could work
     
  3. BigBWolf

    BigBWolf

    Joined:
    Jul 6, 2020
    Posts:
    2
    This has been recommended before and while it seemed like it would work it doesn't. I use a physics overlap for my ground check and it doesn't even get to apply any amount of force before it turns on the nav mesh again because of how fast code naturally executes. It'll apply the force then turn on the nav mesh in the same second.
     
  4. maomaothegreat1

    maomaothegreat1

    Joined:
    Jul 6, 2023
    Posts:
    14
    ok so an extra bool should be all you need
    if (isGrounded&&canJump==true)
    {
    //Turn off navmesh
    //Turn on Rigidbody
    //Add force
    invoke( nameof (ResetJump),small delay)
    }

    // this if statement prob not needed if (isGrounded==false)
    {
    if (ground check function)
    {
    if(canJump==false)
    {
    //Turn off rigidbody
    //Turn on navmesh
    //coroutine to space out
    canJump=true;
    }
    isGrounded=true;
    }
    }

    ResetJump ()
    {
    canJump=false
    }
    should work
    should fix you computer working to fast because of invoke delay.
    if this dose not work you could also try putting the functions in FixedUpdate
     
    BigBWolf likes this.