Skip to content

[Solved][Unity] Use “trigger” to Trigger but Rigidbody Does Not Simulate Collision

Last Updated on 2021-10-28 by Clay

I recently use Unity to develop when a snake game, I want to delete the head game object when my snake head collides with another snake's body, I need to use OnTriggerEnter2D() to set the collision determination.

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.tag == "Snake")
    {
        Destroy(gameObject);
    }
}


However, because I need to determine the snakes collision or not, I use Rigidbody2D to my game object, but this make my snake head and body push each other.

https://clay-atlas.com/wp-content/uploads/2021/06/SnakeEating-GameScene-Android-Unity-2019.4.27f1-Personal_-_DX11_-2021-06-11-11-13-42.mp4

Solution

The solution is very easy, just change the Body Type of RigidBody2D to Kinematic.


There are 3 types of Body Type: Dynamic, Kinematic, and Static.

Dynamic

  • Dynamic game objects are completely driven by physics engine
  • The force, speed, etc. of Dynamic game objects are affected
  • The physic engine handles bounces according to the collision requirements of other objects
  • It is suitable for game characters and mobile game objects


Kinematic

  • Kinematic game objects are completely script-driven, so MovePosition(), MoveRotation() must be processed
  • Kinematic game objects only process collisions with Dynamic game objects; for example, when bouncing, send the OnCollisionEnter() event
  • It is suitable for making game characters more flexible and customized (this is my requirement)



Static

  • There is no real RigidBody component attached, so the physics engine does not work
  • No collisions, cannot use OnTrigger() and OnCollision()
  • It is suitable for game objects that do not want to collide, such as floors and walls

References


Read More

Tags:

Leave a ReplyCancel reply

Exit mobile version