Enemies fly everywhere when they collide with each other
I'm making a game where several enemies are chasing you down and you can defend yourself with bombs. When these bombs hit something, they explode. When colliding with an explosion, the enemies fly away with a proportionate "explosion push force":
// Push me back from the center of the explosion
Vector3 fromExplosionToMe = gameObject.transform.position - objHit.transform.position;
gameObject.GetComponent<Rigidbody> ().AddForce (
fromExplosionToMe * Bomb.ExplosionPushForce * Time.deltaTime,
ForceMode.Impulse);
The trouble is that, sometimes, when that enemy hits ANOTHER enemy while he flies away from the explosion, that other enemy flies away at massive speeds and away from the screen:
I first thought that the problem could be that I was moving the enemies towards the player with translations instead of with forces, so when a force-powered enemy collided with a translation-powered enemy it would result in awkward physics. However, after converting all the translation movements to physics, I'm still having the problem:
// Non-physics-based movement (OLD)
//gameObject.transform.position =
// Vector3.MoveTowards(gameObject.transform.position,
// player.transform.position,
// MOVE_SPEED * Time.deltaTime);
// Physics-based movement (NEW)
gameObject.GetComponent<Rigidbody> ().MovePosition (
Vector3.MoveTowards(gameObject.transform.position,
player.transform.position,
MOVE_SPEED * Time.deltaTime));
Has anyone had a similar experience with collisions that result from other collisons?
Thanks in advance!
Answer by dynamoo21 · Sep 06, 2016 at 11:48 AM
One Way to prevent is using by isKinematic Property of Rigidbody Component. When isKinematic is true the objects will not be affected by physics forces. In actual physics when an object in motion in hits another object in motion/rest, it results in force on participating bodies to the acceleration of bodies which depends on the net force and direction of the acceleration depends on the mass of participating objects. This is Newton's second law of motion. Unity physics behaves the same way.
Other Options: Layer Based Collision Detection:-
Create a Layer : Edit -> Project Setting -> Tags and Layer
Put the Fly GameObject/Prefab in the layer you made via Inspector
Goto: Edit-> Project Setting-> Physics/Physics2D(depending upon game you are making)
Look for Layer Collision Matrix.
Uncheck The Collision for your Layer.
Thank you for your answer, but there are a few problems with it for me:
I need the enemies to be affected by physics, I don't want them to be kinematic. I want them to get thrown way by the explosions and other factors, and fly away with a given force, and I want them to collide with each other in the X-Z plane and not be able to "pile up" together. I want the player to be overwhelmed by 20 enemies running towards him, but I don't want them to get "stacked" together. Everyone should occupy a space and the player should be able to use chokepoints where only a few of them may fit at a time. Think of the comic/movie 300 and how the Spartans forced the Persians to attack them in a small area where their numbers were useless :)
I'm already using physics layers (I discovered them just a week ago or so, they're extremely powerful tools), but I want the "Enemies" layer to collide with itself for the reasons I just stated.
$$anonymous$$y main problem here is that Newton's second law of motion does not apply. If an enemy "A" travelling at 20 $$anonymous$$m/h hits an enemy "B" who is static, "B" may be sent flying away as well with a "Do$$anonymous$$o effect", but at 20$$anonymous$$m/h or less. He shouldn't be sent away flying at 300 $$anonymous$$m/h or something, as it is now.
In any case, thanks for your reply!
In that case what you can do is to, in the script attached to "enemy" game object either Check for the velcoity.magnitude of the object and it is exceeding your desired limit then give a proportional force to in opposite direction else you can limit the velocity using function like $$anonymous$$athf.Clamp()
Your answer
Follow this Question
Related Questions
Recreating 'skin width' functionality on a rigidbody? 0 Answers
Rigidbody randomly going through collider 1 Answer
How to Ignore Collisions Between a Layer and a LayerMask? 1 Answer
Why is my trigger collider acting like a normal collider? 1 Answer
Physics.OverlapSphere not detecting collision! Collision help. 1 Answer