- Home /
OnCollisionEnter() not getting called between two rigid bodies
void OnCollisionEnter(Collision other)
{
print("Called");
if(other.transform.tag=="enemy")
{
//die();
print ("enemy hit");
}
}
i'm very new to Unity,basically building my first game. in my game i have a PreFab(enemy) , that gets created N number of times and is a sphere . i have another sphere, the main character, which when collides with the enemy/s, it should destroy that enemy, but the OnCollisionEnter() does not get called. and both of them are in motion, the player is in circular motion and the enemy in a staright line towards the centre of the circle around which the player revolves. details about prefab and player
PreFab(enemy): sphere, sphere collider,rigidbody player: sphere, sphere collider,rigidbody
any suggestions would be helpful.
Thanks
I'm having the same problem. Both GameObjects have rigidbodies. Other suggestions on this form haven't been very helpful. In addition both my GameObjects are non-kinematic and have box colliders with isTrigger enabled. I've double checked my physics settings and everything in the Layer Collision $$anonymous$$atrix seems to be enabled..
@leftoversalad "In addition both my GameObjects are non-kinematic and have box colliders with isTrigger enabled."
That'll be why then: if you want objects to collide, you want them NOT to be triggers...
Hi, check on your two rigidbody you have a physics collider. Try with is trigger on and is trigger off
There's no "trying" involved: if you want OnCollisionXXX to fire, you turn triggers off.
@tanoshimi @Clem46 even with all triggers off OnCollisionEnter() is not executing
Could it be that I'm missing a component? Here are my game objects:
Name: Bullet
Components:
Sprite Renderer;
C# script (bullet control);
Rigidbody {mass:1, drag:0, angular drag:0.05, useGravity: false, is$$anonymous$$inematic: false, interpolate:none, Collision detection: discrete, constraints: freeze all rotations};
Box Collider {isTrigger:false, material: none, center:(0,0,0), size:(1,1,1)}
====
Name: Player
Components:
Sprite Renderer;
C# script (player control, contains OnCollisionEnter);
Rigidbody {mass:1, drag:0, angular drag:0.05, useGravity: false, is$$anonymous$$inematic: false, interpolate:none, Collision detection: discrete, constraints: freeze all rotations};
Box Collider {isTrigger:false, material: none, center:(0,0,0), size:(1,1,1)}
Answer by SwissCore · May 19, 2017 at 11:37 AM
try: if (other.CompareTag("enemy"))
Or are you sure your tag is "enemy" not "Enemy"? Maybe its only a little typo. :)
Answer by Endi24 · May 20, 2017 at 01:26 PM
It may be that the tag you are looking for is "Enemy" or that you don't actually need the transform, just other.CompateTag("Enemy").
Answer by nullgarden · May 20, 2017 at 07:12 PM
I finally got mine to work by changing other.gameObject.tag == "Enemy"
to access the collider: other.collider.gameObject.tag == "Enemy"
:
void OnCollisionEnter(Collision other){
if(other.collider.gameObject.tag == "Enemy"){
print("hit");
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
On Trigger Enter, Collide with object, specific collision 1 Answer
Nav Mesh Agent para perseguir a un clon 1 Answer
Checking Tag of Trigger Prefab 1 Answer
OnTriggerEnter/OnCollisionEnter and 5.0 3 Answers