- Home /
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
Answer by robertbu · Jun 04, 2013 at 06:45 PM
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.
What I really want is just a burst that weakens as it travels, in a cone-shape. Can this be done with AddExplosionForce? Does it have to be a trigger?
Thanks in advance! :D
It is going to be something like this (untested):
#pragma strict
var radius = 5.0;
var power = 10.0;
var coneAngle = 20;
function Update() {
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
FireGun();
}
}
function FireGun () {
var explosionPos : Vector3 = transform.position;
var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
for (var hit : Collider in colliders) {
if (hit.rigidbody != null && (Vector3.Angle(transform.forward, collider.transform.position - transform.position) <= coneAngle)) {
hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
}
}
}
Note you may want to check the 'tag' of the collider so you only apply the force to the things you want to add force to. This code uses the forward vector of the game object it is attached to.
Thanks dude, you are very awesome, just with a little tweaks to suit my needs, this script is the most organized I have ever seen out of all the others regarding this issue. I had to translate it to C#, though, because that is the language I use in my projects. Thanks again dude!
Thanks a lot robertbu! Your answer and code helped me a lot to get something like the original poster wanted, going in my project. It's thanks to people like you that this community is so great!
Your answer
Follow this Question
Related Questions
Change player movement 0 Answers
Movement problem? 0 Answers
Stop a rotating plane using external force 1 Answer
Add force on camera using AddForce 2 Answers