Search Unity

Forcemode.Impulse keeps applying a force forever

Discussion in 'Physics' started by Paulprog16, Jul 31, 2019.

  1. Paulprog16

    Paulprog16

    Joined:
    Apr 20, 2016
    Posts:
    54
    Hello,

    Sorry if my question is stupid (it probably is), but I am trying apply an impulse force on a ball (like it's being hit by a racquet), so I thought the right thing to do was go with Forcemode.Impulse.

    I am applying the force in FixedUpdate, and the force is applied all the time, so I don't understand how Impulse is different from Force.

    Shouldn't an Impulse Force be applied just once and just stop when it meets another collider?
     
    Last edited: Jul 31, 2019
  2. KouroshX98

    KouroshX98

    Joined:
    Feb 8, 2018
    Posts:
    24
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    An impulse force is effectively the same as "Velocity = Velocity + Force * (1 / Mass)"
    A non-impulse force effectively is the same as "Velocity = Velocity + Force * (1 / Mass) * SimulationTimeStep"

    In other words, an impulse force isn't time-integrated (scaled by the time-step being simulated) and you get all of the force applied to change the velocity. A non-impulse force is time-integrated so you only get a fraction of it.

    The idea being that an impulse force is applied to get a fixed amount instantly irrelevant of the time-steps used for the simulation. A non-impulse force is time-integrated so you typically applied it continuously.

    To hammer this home; if you apply a non-impulse force but before you apply it, scale the size of it up by the reciprocal of the time-step (typically 1.0f / Time.fixedDeltaTime) then it'll be identical to an impulse force.

    It has nothing whatsoever to do with collisions.
     
    Last edited: Jul 31, 2019
    mohamedkaram09 likes this.
  4. Paulprog16

    Paulprog16

    Joined:
    Apr 20, 2016
    Posts:
    54
    Thank you for your replies guys.

    To be honest with you, I don't understand most of what you are talking about here, the reason being that I am still kind of new to game physics and so far I haven't been able to find even one good tutorial, official or unofficial, that goes over the subject in a clear and insightful manner.

    So I will rephrase my question, using the most simple possible vocabulary and I would ask you to please try to reply keeping it as simple as possible:

    When you hit a ball, you hit it just once and you apply only one force in just a fraction of a second. After you applied that force, the ball is gone and you have no way of applying any further force. I mean, you don't follow up on the force you just applied. That's how it works in real life.

    So how can I do that in Unity?

    Using AddForce in FixedUpdate is clearly not working, since the force continues to be applied forever.

    I hope I was able to get my point across.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    So "hitting a ball once" is effectively the same as applying a single impulse force. Do it once, not continually again and again as that's like following the ball and hitting again and again.

    If you're adding a force in FixedUpdate then you're doing that again and again which is something you just said you didn't want to do so don't do that.

    Maybe post some code if you're still unsure? (Don't forget to use code-tags if so).

    Hope this helps.
     
  6. Paulprog16

    Paulprog16

    Joined:
    Apr 20, 2016
    Posts:
    54
    Would that mean implementing the AddForce line outside of FixedUpdate? If so, where?

    I had it understood that everything that involves physics was supposed to go inside FixedUpdate, hence part of my confusion.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    It's not about where you call it, it's about calling it multiple times if you only want to call it once. I have not seen your code so I cannot see if you're calling it again and again. All I can say is that FixedUpdate is obviously called continually so hopefully you have logic that ensures you're not doing it each FixedUpdate.

    Posting a code snippet might clarify what you're doing.
     
    Paulprog16 likes this.
  8. Paulprog16

    Paulprog16

    Joined:
    Apr 20, 2016
    Posts:
    54
    Ok, I figured this out right after I submitted my last reply. What you point out is exactly the stupid mistake I was making. I put the line of code in FixedUpdate but right out there in the air, so it was effectively being called all the time.

    Now I put the line inside an If Statement and it seems to work the right way.

    Thank you!
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    No problem, glad you got it working. Like the post if it helped, I collect them like coins. ;)
     
    KouroshX98 and Paulprog16 like this.
  10. Paulprog16

    Paulprog16

    Joined:
    Apr 20, 2016
    Posts:
    54
    Done : )
     
    MelvMay likes this.