Applying a force to multiple objects at once...

I wanted to create a sort of force-gun that forces everything away from the area in front of it, similar to Half Life’s Gravity Gun. How can this be implemented? I have already thought of 2 ways, but waiting confirmation from anyone.

  • The first was instantiating an object that will fly forwards with a powerful RigidBody force, pushing everything in front of it, then destroying itself after a period of time.
  • The second idea was a cone-shaped collider in front of the player, and by the use of an OnTriggerEnter, I can apply a force to the objects in the direction the camera is facing…

But the question is: How do I apply a force in the direction the camera is facing?

Thanks in advance,

John

There are two parts to this problem. The first is identifying the targets. How you do this will depend on the nature of the gun and the nature of your targets. You could use a Physics.SphereCastAll if you gun shoots a tube of force. If your gun shoots a cone of force, you can use Physics.OverlapSphere() to identify potential targets, then use Vector3.Angle() between the forward of the gun and each potential target to see if it is in the cone of force.

As for applying force, it will depend on what you want. Rigidbody.AddExplosonForce() will give you a force that falls off with distance from the gun, so that object further away will be less impacted. Or you can just apply a fixed amount of force in the direction the gun is aiming.