I am creating a 2d side scroller Zombie Shooting car race game. In this game I need feature through which player can shoot the zombie from the car as well. The problem is that when I apply force to shoot the bullet toward right, the car also gets effected by that force i.e it just moves allot like in reaction of the bullet shot. I just don’t need this behavior. I want the car to remain calm and just shoot.
Is there any way to exclude one perticular rigidbody i.e the rigidbody attached to the car from getting the force that the bullet rigidbody is getting ?
Maybe you could make the car rigidbody static while the force is getting applyed?
car.getComponent(smaller)Rigidbody(greater)().isKinematic = true;
//shoot bullet
car.getComponent(smaller)Rigidbody(greater)().isKinematic = false;
and if you want to keep the cars velocity (if it is moving) then use this:
Vector3 v = car.getComponent(smaller)Rigidbody(greater)().velocity;
car.getComponent(smaller)Rigidbody(greater)().isKinematic = true;
//shoot bullet
car.getComponent(smaller)Rigidbody(greater)().isKinematic = false;
car.getComponent(smaller)Rigidbody(greater)().velocity = v;
Sorry for this mess, but I needed to replace the “>” with (smaller) and (greater), because else they’d get hidden
From my understanding, you are instantiating a bullet and then applying force to the bullet game object which also has a rigidbody on it. If that is the case then the problem is when you are applying force to the bullet rigidbody it also affects with the car as is it is close to the car at the time of applying force. To avoid this problem you need to layered out your car and bullet separately. Change the layer of the car from the inspector to “Car” (of course it wont be their, you have to add it manually) and change the layer of the bullet prefab to “Bullet”. Then from Edit>Project Settings>Physics 2D mark out the collision between the “Car” and “Bullet” layer. That should solve the problem. As an extra check make sure both your car and bullet have a 2D collider on it.