- Home /
Applying force to a rigidbody
I am writing script for a prototype game and I have run into an issue with force. I have a bullet prefab with a rigidbody and a box collider. The rigidbody is not kinematic and is affected by gravity. I instantiate this bullet with a pretty standard instantiate function, spawning it in front of my characters face (because why can't I shoot a bullet out of my face?) and apply force with bullet.rigidbody.AddForce(transform.forward * 2000)
. When I hit space (my fire button) the bullet gets spawned, but only receives a small amount of force and falls to the ground rather quickly. No matter what value I use in place of the 2000, I get the same effect. Thanks in advance for any help.
No, there is no collision occurring, but after a little testing, I found that when I comment my AddForce line, I get no change in effect. $$anonymous$$aybe transform.position in the AddForce parameter is set to 0? And if that's the case, no matter what number I multiply it by, it would obviously have no effect.
Answer by Neinth · Sep 17, 2013 at 10:30 PM
Not sure if this is answered or not,but the problem is "addForce" is a one time deal.In order to keep momentum up you'd need to "addForce" each frame.
You could also reduce drag,which 1 single "addForce" would start it moving,and it would slow down as it moved relative to the drag amount.You could also add a method to counteract gravity so that it still falls,but based on current force applied,add force upwards just under the gravity settings of project.0 drag would make it never slow down over distance/time unless you changed it's force manually over distance/time.
I do believe you could use "setVelocity" as well,not 100% sure how that works with drag and gravity though.
For a projectile type I'd track distance it has traveled,and set the "z" axis(how fast forward) and "y" axis force(used to counter gravity) based on that.This would give you a controllable bullet drop rate over set distances,as well as base the damage when it hits something on distance.Heavier bullets would drop faster,while maintaining forward momentum,and lighter ones would lose forward momentum faster,but drop slower.
Then again I am still learning,and that might be over complicating things.
Answer by vulgarknight · Feb 08, 2013 at 09:40 PM
The mass value is set to 0.01, not very much, but I'm not very familiar with mass values in Unity. Is that a small value? Maybe something else is affecting the bullet mass?