I have a simple project with a player object (cube + IsKinetic rigidbody + box collider) that is moved with left mouse clicks, and right mouse clicks trigger an explosion force to move enemy cubes away using AddExplosionForce().
The enemy cubes have a rigid body + box collider (without IsKiinetik checked). The boxes always chase the player using the following code in FixedUpdate:
I can move the player around with left mouse button and the enemy cubes chase him just fine. But as soon as I execute code in the player script that calls rigidbody.AddExplosionForce() , the explosion pushes the enemies slightly away as expected, but the enemies come to rest from the explosion and never chase the player again even though I am calling MoveUpdate() above in the FixedUpdate() method. Once this happens, if I click the left mouse button and move the player, the enemies will move (only one time) but maintain the explosion radius distance and never get closer.
Does anyone know why AddExplosionForce() would stop my MoveUpdate() on the enemies from working? What I expect to happen is the enemies move “radius” distance away, but then start moving back towards the current player again once the explosion is over.
Thanks!
In case it matters, here is the explosion code executed from Update() on the player object:
if (Input.GetButtonUp("Fire2")) // Fire2 button released
{
Vector3 exposionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere(exposionPos, 5.0f);
foreach( Collider hit in colliders )
{
if ( !hit ) continue;
if ( hit.rigidbody )
hit.rigidbody.AddExplosionForce(20.0f, exposionPos, 5.0f, 0f, ForceMode.Force); // Force, pos, radius
}
}
Note: If I keep spamming right mouse button explosions until all the enemies have stopped at the 5.0 radius and are no longer effected by it, when I move my player, all the enemies will follow him, but they will maintain the 5.0 unit distance… In other words if they are pushed 5 units away and form a circle around the player, they will remain in that circle pattern when following me and never get closer than 5.0 even though I am no longer creating explosions at that point.
So it looks like iTween.MoveUpdate() does move them, but they never get closer than 5.0 unless I never use any explosions, in which case they will come right up to me.
Make sure you have some drag on your enemy rigidbodies, otherwise the physics forces will not diminish over time.
Having zero drag would probably cause what you are seeing, where the objects get to a point where the forces and the iTween are balancing each other out at a certain distance.
==== Rigidbody docs
Drag = How much air resistance affects the object when moving from forces. 0 means no air resistance, and infinity makes the object stop moving immediately.
Angular Drag = How much air resistance affects the object when rotating from torque. 0 means no air resistance, and infinity makes the object stop rotating immediately.
=== Then down in TIPS it says:
A low Drag value makes an object seem heavy. A high one makes it seem light. Typical values for Drag are between .001 (solid block of metal) and 10 (feather).
Doesn’t that contradict itself? It defines drag saying that 0 is none and infinity is practically un-movable. Then in the tips it tells you that larger numbers mean lighter objects. I would think a block of steel (.001 in the tips) would have more friction then a feather (10.0) which is extremely light.
No, I don’t think they are contradictory. An object with low drag seems heavier than one with high drag.
For example, imagine the difference between throwing (or dropping) a bowling ball and a feather. The ball would travel quite far (low drag), while the feather would stop almost immediately (high drag).