G.T.
G.T.

Reputation: 562

Unity collision bounces off, but no OnCollisionEnter

I have a problem with Unity collision system.

I'm trying to add some 3d objects(rocks) to my scene with mesh collider(convex mesh collider ticked), but no rigidbody. I have a bullet prefab with rigidbody and sphere collider. I attached a script to the bullet prefab, that OnCollisionEnter it should destroy the bullet prefab and play an explosion particle.

Now the problem is that when I shoot the bullet towards the 3d object (rock) it bounces off instead of exploding( aka no OnCollisionEnter called). With other colliders like box, capsule or terrain collider it works just fine.

I tried to add a rigidbody to it, after which it starts working, OnCollisionEnter is called and the bullet explodes, but the problem with this is that I can move the 3d object with physics which I shouldn't be able to. To solve this i tried to constraint in the inspector the position and rotation, which I thought solved my problem, because the bullet explodes on contact and the 3d object won't move, but now my other problem is that I can walk through the object...

Any idea how to solve my problem? So that I would have a 3d object with meshcollider calling OnCollisionEnter on contact with a sphere collider, but not letting me pass trough it?

Edit1: Forgot to mention that I already tried IsKinematic same rule applies, bullet bounces off, but no OnCollisionEnter called.

Edit2, somewhat solution: I ended up duplicating the object, removing mesh renderer from second one scaling it a little bit and removing Gravity and IsKinematic, this way that layer triggers OnCollisionEnter and my bullet explodes, the original is let at IsKinematic witch don't let me go trough the object and because the other layer is a little bit bigger the bullet can't bounce off. Even though this seems to work, for me it seems a little bit over kill and probably gives me an overhead, is there any better solution for it?

Upvotes: 1

Views: 7483

Answers (3)

G.T.
G.T.

Reputation: 562

I found out what's causing the problem. I set the sphere collider radius way too small (0.025 instead of 0.5, dunno how xD my fault) and for some strange reason with dynamic continuous physics it was detecting collision but no event was raised. Only when collided with terrain or basic shapes, but not with mesh collider.

It's an interesting behavior, but at least now I know what I did wrong.

Upvotes: 2

DreamBaked
DreamBaked

Reputation: 1

Something i overlooked that might help somebody. I was trying to detect 3D collisions when my game was using 2D physics. So for example if your using BoxCollider2D make sure you use OnCollisionEnter2D(Collision2D collision)

Upvotes: -1

Boomer Rogers
Boomer Rogers

Reputation: 1015

If you want to disable the physics of the object ( and I'm not sure if you've done this already ) try clicking on the object, go to the Rigidbody component, and disable the gravity check box ( assuming you don't want gravity applied ) and enable the "is Kinematic" check-box. Kinematic tells unity to not apply physics to your object.

edit: also make sure to undo the fixed positioning that you currently have, if is Kinematic is working for you, that is.

Upvotes: 1

Related Questions