Force of bullet - Distance to Object

Hi guys,

I have searched for this but I can’t find it mentioned (though that could be because I am searching for the wrong thing)

I am trying to set the force of a bullet depending on the distance to the player. I need to work in reverse though of course so that the bigger the distance the less force is applied.

I found mention of the inverse of this which is something like this…

force = 1 / distanceToPlayer * distanceToPlayer * bulletForce;

This just gives me the initial bullet force though.

Not being a math’s genius I was hoping to ask is there a way to inverse the distance?

I hope I have explained this well enough!

Thanks for your time.

Sincerely,

Bruce

I’m not sure what you are looking for here. If you are letting the physics engine do the work, how much force will be applied with depend on the momentum of the bullet. So one easy way to solve this problem would be to setup some drag for the projectile in the Rigidbody component. The further it travels, the slower it goes.

If for visual reasons you did not want to slow the projectile, you could (in theory since I’ve never tried it), scale the mass over time. For a non-linear decline you can do something like each frame or each FixedUpdate():

rigidbody.mass = rigidbody.mass * 0.998;

Here is the calculation I think you are asking for:

if (distanceToPlayer < maxDist)
    force = (maxDist - distanceToPlayer) / maxDist * forceFactor;
else
    force = 0.0;

Where max distance is the maximum effective distance of the projectile. This gives a linear dropoff.