Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Add Force Impulse Help

Discussion in 'Physics' started by MetalDonut, Jul 26, 2020.

  1. MetalDonut

    MetalDonut

    Joined:
    Feb 7, 2016
    Posts:
    119
    Hi Everyone,

    I'm using AddForce with Impulse mode on a ragdoll (using PuppetMaster) by initiating the ragdoll and then applying a force for one step in FixedUpdate by using a boolean (addForce).

    Code (CSharp):
    1. void FixedUpdate()
    2. {
    3.     if(addForce)
    4.     {
    5.           hit_RB.AddForce(forceDirection * hitForce, ForceMode.Impulse);
    6.           addForce = false;
    7.     }
    8. }
    I'm not sure if this is the right way to use AddForce with Impulse but it only seems to work in FixedUpdate and it continuously adds force if I don't use a boolean to turn it off after.

    This works perfectly for all but one occasion where I want to add force. This is when a character falls backward off a ledge. I make the character ragdoll and want to apply a bit of backward force but it completely ignores the force. As the force is on a ragdoll and is called the same way as other ragdoll scenarios I can't work out why it doesn't work on this one occasion. If I remove the line "addForce = false;" then it does add the force in this scenario (albeit every fixed step which is obviously not what I want).

    So my question is a) am I even using AddForce properly, and b) is there a reason why it won't work when only being called in a single step in one scenario even though it correctly adds force in a single step in other scenarios?

    PS - I can confirm that the scenario where it doesn't work, I've not got the rigidbody set to isKinematic and I'm not manually applying velocity to the rigidbody. I also tried switching ForceMode to VelocityChange and Force but still having the same issue.

    Thanks in advance for any help or guidance here!
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    1,856
    a) Yes, that's a correct way to apply an one-time impulse. I assume forceDirection is an unit vector.

    b) I can't help here, sorry. I don't understand the situation, and don't know if the character is receiving other interactions. All AddForce calls within the same FixedUpdate are accumulated and applied together at the next physics update.
     
  3. MetalDonut

    MetalDonut

    Joined:
    Feb 7, 2016
    Posts:
    119
    Thank you. Well, it's good to know I'm at least doing it right. And yes, force direction is a Vector3.

    I've notice if I trigger the force around a key press and press the button at the point where the character is landing the force correctly applies. Not sure if that helps, but it's helpful to know I'm not doing something totally wrong as I keep testing. Thank you!
     
  4. MetalDonut

    MetalDonut

    Joined:
    Feb 7, 2016
    Posts:
    119
    I found a solution that seems to work so just wanted to share for anyone that stumbles across a similar issue...

    I turn on my addForce bool via a CoRoutine which waits for the fixed update frame to end first. This now correctly adds force in all scenarios.

    Code (CSharp):
    1. IEnumerator DelayedForce()
    2. {
    3.     yield return new WaitForFixedUpdate();
    4.  
    5.     addForce = true;
    6. }
     
    Edy likes this.
unityunity