- Home /
Changing transform.localScale but OnCollisionEnter isn't executed
Hi, I've tried to make the description of my problem as clear as possible, but if there's any more information you'd like, please ask. The setup:
I have a Grenade GameObject with a script DetonateAfterDelay.cs on it that causes it to destroy itself and spawn a GrenadeExplosion object. The GrenadeExplosion object has two scripts on it, ExpandAndDie.cs and AddForceOnCollision.cs.
Here are the two scripts on the GrenadeExplosion object:
The ExpandAndDie script uses a coroutine to Lerp the transform.localScale over about 0.15 seconds, effectively making the GrenadeExplosion a rapidly expanding sphere. It looks very simple and nice. :)
The Player has a CapsuleCollider and Rigidbody attached. The GrenadeExplosion has a SphereCollider.
The trouble is that the GrenadeExplosion object only registers a collision with my Player object when the Player is moving. When the Player is standing still, the explosion object passes through the Player and does not trigger the OnCollisionEnter code found in the AddForceOnCollision script. In fact, even the Debug.Log statements I have in that script do not trigger.
I don't know why this is the case, but I think it might have something to do with how I change the GrenadeExplosion object's localScale.
Perhaps using a coroutine (GrowAndDie()) is part of the problem? But I can't figure out a way to solve this problem without using a coroutine.
Do I need to use an animation to expand the sphere for the OnCollisionEnter script to work properly? I'm in the process of trying this method, but I don't know if it will solve the problem, and it's proving more complicated than I expected. I would prefer to stick with the simple procedural method of lerping localScale, if possible.
Any help, advice or direction is greatly appreciated. Thanks.
Answer by LucTremblay · Aug 10, 2013 at 01:12 AM
At a gance, this might be caused by an optimization of the physics system, where un-moving rigidbodies are not tested together. I think that scaling the root of the rigidbody does not provoke a 'movement', and therefore the collision checks are not being done. Scale and physics rarely get along in game engines :)
Try
rigidbody.WakeUp();
right before your yield.
wesome, thanks, that worked. I'm going to keep playing around, because I think there must be a way to set things up that makes the WakeUp call unnecessary. Thanks again.
For what it's worth, I'm pretty sure scaling as you do is a special case of the physics engine, and WakeUp was expressedly made for that kind of situation. I can't think of a cleaner alternative, other than doing your own collision checks.
Your answer
Follow this Question
Related Questions
How to end a coroutine early 0 Answers
OnCollisionEnter event seems to be called late. 3 Answers
OnCollisionEnter called once even with multiple contacts 1 Answer
OnCollisionEnter inside FixedUpdate? 1 Answer
Collision Query 1 Answer