- Home /
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
Answer by robertbu · Jun 18, 2013 at 04:01 AM
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.
$$anonymous$$any thanks Robert!
There is no actual bullet, it is done with ray casting and parameters (such as force and distance) are set via the weapon script. Sorry, I should have explained that at first.
So the second part of your response is perfect!
Thanks again :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613104357im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotational Force 0 Answers
How to get inverse Force? 2 Answers
calculate the estimated distance travel based on the Force applied to Rigid Body 1 Answer
How to disable the physical reaction of a distance joint 2D? 1 Answer
Gravity within range 1 Answer