- Home /
Adding force to a bullet as it hits object?
The bullet is a simple prefab with a collider, rigidbody and mesh. I want to shoot boxes and such in the game and have them be "pushed" in the opposite direction of the bullet's path. Is there any method to do this?
Just let the engine do it for you. Just make your boxes a rigidbody, add a mass too and let them fly the way they should.
Trying to do it on your own is real hard and takes a lot of physical parameters. Just make it easy.
Answer by eddyzy · Sep 08, 2013 at 03:27 PM
void OnCollisionEnter(Collision collision)
{
if(collision.tag=="stuff to shoot at")
collision.rigidbody.AddForce(x,y,z);
}
the boxes should have a rigidbody component as well as a colider;
x,y,z the axis you vant to add force e.g rigidbody.AddForce(0,0,10);
you need to tag the boxes with a new tag or you could get rid of the if statement
you should attach this to the bullet prefab
I found a small issue. It applies the force on a "world axis". I need to force to be applied in the direction opposite of the bullet path. For example I shoot a cube and the box moves east. I shoot it again from a completely different angle and it still moves east. This is a bit of a problem for me. How can I fix this?
rigidbody.AddRelativeForce(0,0,10);
if that doesn`t work just lower the mass of the boxes rigidbody if your bullet has higher mass it shold move the boxes.
Yeah I ended up doing option 2 because relative force doesn't change a thing. It still works though.
Answer by DoctorMoney · Sep 08, 2013 at 03:30 PM
Make the boxes have rigidbodies and the bullet should push the boxes with some force as it collides with them.