- Home /
How can I affect all colliders within a certain radius?
How can I affect all objects currently in a collider trigger's space?
I want to be able to constantly affect all the objects' velocities in a spherical radius around the player (excluding some), whenever the player holds left click, but if he lets go then everything returns to normal.
The unity scripting reference page isn't so helpful.. but i think Collider.OnTriggerStay is the way? There is no explanation on how to use it, especially in my case.
Thanks a ton!!!!
Answer by duck · Feb 19, 2010 at 04:52 PM
You might be better off looking at Physics.OverlapSphere.
This function returns an array with all colliders touching or inside the sphere, and sounds pretty much ideally suited to your question.
You'd then want to loop through the list of returned colliders in order to apply the force to them, and since you're applying physics forces, all this code should go in your FixedUpdate() function. Something like this:
var colliders : Collider[] = Physics.OverlapSphere( transform.position, whateverRadius );
for(var hit in colliders) { hit.rigidbody.AddForce( whateverForce ); }
Answer by nihilocrat 1 · Feb 19, 2010 at 03:20 PM
Yes, OnTriggerStay is one way to do it. You could also do OnTriggerEnter, set some kind of scalar on all the objects (like speed or whatever), and then set it back to the original value with an OnTriggerExit.
Answer by PrimeDerektive · Feb 24, 2010 at 03:20 PM
I know its probably sloppy, and lazier than doing Physics.Overlapsphere, but I sometimes like to attach a sphere collider to whatever I am putting trigger code on and hiding the mesh renderer in the inspector.
Why would you do this? To have a visual of the sphere in the editor?
Your answer
Follow this Question
Related Questions
Help with collision detection with specific colliders 1 Answer
Collision problem in C# 4 Answers
Problem with the collider(I think) 0 Answers
How to restart scene with trigger? 1 Answer
Trouble with light switch 3 Answers