x


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!!!!

more ▼

asked Feb 19 '10 at 01:45 PM

Yisreal gravatar image

Yisreal
115 8 9 12

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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 );
}
more ▼

answered Feb 19 '10 at 04:52 PM

duck gravatar image

duck ♦♦
40.9k 92 148 415

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Feb 19 '10 at 03:20 PM

nihilocrat 1 gravatar image

nihilocrat 1
17 2

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Feb 24 '10 at 03:20 PM

PrimeDerektive gravatar image

PrimeDerektive
3.1k 57 64 84

Why would you do this? To have a visual of the sphere in the editor?

Mar 23 '10 at 02:38 PM Ben Throop
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2482
x1682
x980

asked: Feb 19 '10 at 01:45 PM

Seen: 2822 times

Last Updated: Feb 24 '10 at 11:31 AM