Knockback to specified distance

I read many threads/videos for knockback implementation and most of them suggest using AddForce to implement knockback.
But AddForce cannot control the distance pushed. How to implement a knockback to a specified distance?

Hmmm. I have not done something like this myself but I have been working with a rigidbody/addforce recently.

What about logging your current position at the start of the “Knockback” Then removing/canceling the addforce once a cetain distance is reached away from that point on any axis? (If you log your “original position” at start of knockback and feed in the position of the “source” of knockback you could also make it so the knockback is opposite the direction of the “source”.)

EDIT: Also just realized a real stupid way to do it that is just as effective. Facepalms. Basically just make your “addforce” amount a public variable and have it fire ONLY ONCE. Then tune that variable VIA the inspector to knock back your char as far as you intend him/her/it to go.

The pros of this method are the fact that it uses “Addforce” as actually intended. If your char is standing on a slippy surface like ice, they would keep on keeping on. If they happen to be on a solid surface, like stone. They would only go back so far and stop because of friction. (Of course you NEED to define what ice or stone is to the game, but that is easy enough.)
The cons are simple. You do not get as much direct control over what happens.

But with great power/control. Comes great responsibility. Be sure you are ready to shoulder that responsibility if you decide to take direct control. Otherwise you are making your own job harder (But better arguably.)

This will require some algebra.

You could use parametric equations to model the trajectory of your projectile. See the “Modeling projectile motion” section of this document to get an idea about it: Classzone.com has been retired | HMH

Using those equations you can insert your gravity, the launch and landing heights, your launch angle, and your desired x distance, and figure out what your initial velocity should be. You want to find the velocity such that your horizontal velocity times the time the projectile is in the air will equal the desired horizontal distance. What this velocity is will depend on your launch angle. If you use 45 degrees, you will get the maximum possible distance for the smallest initial velocity. Once you have that, you can use the appropriate ForceMode with AddForce to just set your knocked-back object’s velocity appropriately.

Note that Unity’s physics engine uses discrete simulation timesteps so this method will not be 100% accurate.

That would cause the projectile to land at the desired distance. But unless the collision with the ground is fully inelastic, it won’t necessarily stop at that same distance.

If your game uses physics at all, then having knockback work on a fixed-distance basis is pretty weird. It implies that heavy people can be pushed back just as far as light people, and that you don’t slide back any farther on ice than on gravel. Having knockback work on a fixed-amount-of-force basis is much more natural.

(And obviously if you’re NOT using physics, then you shouldn’t be calling AddForce for ANY reason.)

But if for some reason you truly need a knockback to reliably get to a specific distance, you may need to fudge the physics and write your own script that just keeps moving the target backward (in some physics-breaking way) until they reach the appropriate distance.

1 Like

Basically what I said to do originally, the edit I added is the other method. (I am a total noob though and am sure there are many more. Ways to do it)