- Home /
Scaling a collider to set an event
Im trying to create a visual effect where a bunch of cubes play an animation when a growing spherical trigger engulfs them.
Let me explain further...
various scattered cubes are laid out. they all have a script that has collision detection on them that tells them to play an animation once the collision happens.
function OnCollisionEnter(Collision : Collision){
if(Collision.gameObject.name == "BlockBuilder"){
Debug.Log("Entered Trigger");
}
}
Then there is a sphere collider that has an animation that scales its radius to a huge value over 2 secs.
In theory, what should happen is once a cube is inside the sphere because it has scaled over it, the script should run - but it's not. The only way I can get a detection to happen is if I put rigid bodies on each but then the sphere just pushes the cubes along.
I've tried using a trigger, but to do so I need to place the script on the sphere object and then it will require a giant list of pre-defined objects to check to see which cubes to affect, which I don't want - I want it to be dynamic.
any ideas on how to do this? the easiest way I can think of is somehow making the rigid body not affect anything, is that even possible?
thanx!
Thank you so much for helping out! so here is the script that works:
function OnTriggerEnter(Col : Collider)
{
if(Col.gameObject.name == "BlockBuilder")
{
Debug.Log("Entered Trigger");
}
}
What I needed to do was make it a trigger so that it doesnt push anything around, but still leaving the rigid body there. It works like a charm now! Thank you so much for the help!
Answer by Jesse Anders · Jan 08, 2011 at 09:11 PM
Does the code you posted actually compile? (I haven't tried it, but I'd expect the variable name 'Collision' to generate an error. I don't use UnityScript though, so maybe there's something going on there that I'm not aware of.)
Regarding the problem you describe, I'd expect that if you made the sphere collider a trigger and then used OnTriggerEnter() rather than OnCollisionEnter(), it would work. Have you tried that?
yeah, it compiles and works - as long as I turn on rigid bodies everywhere. but like I said, the problem that I get is that the big sphere ends up just pushing the cubes around (even though the collision happens anyway). Basically you end up seeing the event happen but at the same time they get pushed around.
I have tried with a trigger but could not get it to work. $$anonymous$$aybe I'm using the formatting badly. I have no clue. The only reason why I have been focusing on collision though is because I assumed that the trigger itself is the one that supposed to have the OnTriggerEnter function on it.
I think both objects involved in a 'trigger' collision receive an OnTriggerEnter() message (you can check the docs to make sure). In any case, although I haven't tried it, I think making the sphere a trigger should work; if it's not working, you likely have something set up wrong. (Also, I'm a little surprised that UnityScript will let you give a variable the same name as a type, but like I said, I don't use US so I might be missing something there.)
Ok, this is awesome. I got it working with the trigger. It looks like you still need the rigid body but once you make it a trigger it doesnt move anything around anymore. Thanx!