Search Unity

RigidBody AddForce Only Works Sometimes

Discussion in 'Physics' started by vegasanx, May 7, 2019.

  1. vegasanx

    vegasanx

    Joined:
    Apr 7, 2019
    Posts:
    31
    I have enemies that use Unity's NavMesh but I want to have Rigidbody force to knockback the enemy when it's hit by an attack.

    So when the enemy is hit:

    public override void AddForce(Vector3 Force)
    {
    NavMeshControl(false); // Turns off navmesh update position and rotation among other things
    Rigidbody.isKinematic = false;

    IsAddForce = true;
    ForceToAdd = Force;
    }

    I used to add the force in this method but I moved it to FixedUpdate to see if that would fix my problem (it has not):

    private void FixedUpdate()
    {
    if (IsAddForce)
    {
    Rigidbody.AddForce(ForceToAdd, ForceMode.Impulse);
    IsAddForce = false;
    }
    }

    I've used Debug.Log to confirm that add force is being called when it's supposed to all the time. Sometimes it works perfectly and the enemy is knocked back, however, sometimes it does not work and the enemy remains in place.

    I have confirmed that the nav mesh agent is not being turned on during the knockback and isKinematic remains false.

    I've tried everything I could think of to fix this problem and I'm stumped.

    Recap:
    - yes, the enemy has a rigidbody component
    - yes, IsKinematic is set to false
    - yes, AddForce is being called when it should be (and it's called in FixedUpdate)
    - the force is about the same each time it is hit and it works sometimes so the force applied is enough to move the enemy (drag and other rigidbody variables don't change)
    - the force's direction is set to propel the enemy away from the point of impact (and again I debug.loged the Vector3 force and it's about the same each time when it works and when it doesn't)

    All of the Googled answers for "Rigidbody AddForce not working" have one of the above solutions but I've tried them all and it still doesn't work sometimes. I've tried debug.logging all the variables I can think of that might affect the knock back and they remain the same whether it works or not.

    Please help, I must be missing something.

    Edit: Another thing - my enemy has root and non-root motion animations. When it is hit, it should crossfade into the hurt animation (which is non-root motion) but the animation that is playing before that may or may not have root motion. I'm thinking that might have something to do with it but I'm not sure how I can debug.log the current state name (as far as I know, you can't). So I don't know how I can even figure out if this is the problem or not, let alone how to fix it.
     
    Last edited: May 7, 2019
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    I'd try moving the isKinematic = false line to the FixedUpdate part, right before AddForce. If the problem persists, I'd wait a physics frame between both lines.
     
  3. vegasanx

    vegasanx

    Joined:
    Apr 7, 2019
    Posts:
    31
    @Edy Thanks! Waiting a frame worked!
     
    Edy likes this.