Search code examples
unity-game-enginecollision-detection

In Unity 2D, how do I achieve Non-trigger collsion that doesn't "physically" move the rigidbodies?


I have a bullet game object and an enemy game object.

  • I want to have the ability of getting the collision point/contact, which's why I think I need the collider of the bullet to not be a trigger collider - this way, I want to have the bullet bounce off the enemy. At times, the bullet will need to bounce off of rectangular objects (not a problem), but I also want the bullet to bounce off of elliptic objects (the problem).
  • I want the bullet not to "physically" push the enemy, which is why, intuitively, I should set the bullet's collider to be a trigger collider.

When the bullet's collider is a trigger collider, I seemingly don't have a way to implement the bouncing. But when the collider's not a trigger collider, it pushes the enemy object.

There's no code errors, but I think the code hasn't got anything to do with the problem.

I suspect the problem may have something to do with the Physics Collision Matrix.

EDIT

It seems that raycasting would be the solution to the problem, as Leoverload proposed. My problem became a different problem now, so I'll close off this thread, and open a new one.

Thanks for the help, @Leoverload ! :D


Solution

  • For the position of the hit it's very easy. You must have 2 colliders and 2 Rigidbody and then you can simply add a Void OnTriggerEnter2D(Collision Coll) and inside it check for the tag : if(coll.compareTag("surface")) and inside you can get the position with collision.transform.position. The collision matrix on the Physics menu only allows to remove Collision between certain layers, it's useful if you want the enemies not to push eachother!

    If you don't want the player pushed you can change the collider to trigger, and destroy the bullet just after the collision with the same method as before with a void OnTriggerEnter2D and the compareTag. In this way it will look like it touches the enemy and explode, but it won't actually touch anything. If you want to add knockback to the player then you could do a simple AddForce based on the direction of the bullet and the work is done!

    If you want to avoid the bullet to move walls you can set walls to static and it should work fine