It is reorganized mainly by referring to the following articles in Unity’s official documentation. Docs.unity3d.com/Manual/Phys… Docs.unity3d.com/Manual/clas… Docs.unity3d.com/Manual/clas…

In Unity physics, rigid bodies are used to accept various forces and have speed to control the movement of objects. Collider is used for the interaction and collision between objects, thus affecting the movement of objects. Rigid bodies are often used with colliders to simulate the real world.

The rigid body

  • It will be moved by forces like gravity, but it will not be deformed.
  • Transform should not be used to move, rotate, and scale objects with rigid bodies. MovePosition, rigidBody. MoveRotation, rigidBody2d. MovePosition, rigidBody2d. MoveRotation should be used to move.

Rigid BodyType BodyType

Rigidbody type is an important property of rigidbody.

  • The act of determining movement (position, reversal)
  • Determines how collisions interact, if there are collider components
  • Changing the BodyType is a performance-intensive operation

There are three types.

Dynamic Dynamic

  • The default type
  • It’s affected by gravity and additional forces that change its speed.
  • The type with the highest performance cost.
  • Can be moved by MovePosition, MoveRotation.
  • We can use velocity to change speed.
  • It’s used to simulate moving objects.

Kinematics of Kinematic

  • It’s not affected by the force.
  • Performance costs less than Dynamic and more than Static.
  • Can be moved by MovePosition MoveRotation.
  • We can use velocity to change speed.
  • It’s used to simulate things that don’t move most of the time, but very rarely need to move. Such as the door.

Static Static

  • The physical system does not calculate the object without being affected by forces.
  • The type with the lowest performance cost.
  • Cannot be moved by MovePosition MoveRotation.
  • You cannot change speed with Velocity.
  • To simulate objects that never move.

Set the rigid-body type in Physics2D

The Rigidbody2D component in Physics2D has the BodyType attribute but not the IsKinematic attribute.

  • Dynamic: Add Rigidbody2D and set BodyType to Dynamic.
  • Kinematic: Added Rigidbody2D and set BodyType to Kinematic.
  • Static:
    • Add Rigidbody2D and set BodyType to Static.
    • There is no Rigidbody2D component, but there is a Collider2D component. Equivalent to having a hidden Static Rigidbody2D.
      • It’s easy, you don’t have to add a rigidbody and set the type.
      • It’s not as efficient as explicitly adding rigid bodies.

Set the rigidbody type in Physics3D

Rigidbody in Physics3D has an IsKinematic property but no BodyType property.

  • Dynamic: Rigidbody is added without selecting IsKinematic.
  • Kinematic: Add Rigidbody and select IsKinematic.
  • Static: Collider component without Rigidbody.

The collision body

  • They collide, and they move because of the collision.
  • Defining the shape of this GameObject for collision detection triggers the corresponding collider callback function.
  • The collider is invisible, and the shape of the collider does not have to match the shape of the actual mesh.

Corresponding to the three types of rigid bodies, colliders also have three types of colliders depending on the added rigid body type.

Static Rigidbody Collider

  • A collision body
    • No rigid body
    • Or the rigidbody type is Static.
  • No force or impact
  • Can collide with other colliders, but won’t move on its own. The physical system will be optimized.
  • Try to avoid enable, disable, move, and scale during the game, otherwise there will be a large performance consumption, or even errors.
  • Kinematic Rigidbody Collider should be used if they are stationary most of the time, but in some cases they need to move.

Dynamic Rigidbody Collider

  • We have colliders, we have rigid bodies, and the rigid bodies are of type Dynamic.
  • The most common ones are affected by forces and collisions.

Kinematic Rigidbody Collider

  • There are colliders, there are rigid bodies, and the rigid bodies are Kinematic.
  • The position can be moved by a Transform
  • No force or impact
  • This Collider can occasionally be moved, enable, or disable, but most of the time is the same as Static Collider.

The trigger

  • The collider becomes the trigger when isTrigger is checked
  • The main difference with the collider
    • Other colliders can pass through the collider without being blocked.
    • When passing through the collider, the corresponding trigger callback function is fired.
  • If isTrigger is selected, the three types of triggers correspond to the three types of colliders.

Collider collision rules

  • Colliders with Dynamic rigid bodies and other colliders, they have callbacks.
Collision detection occurs and messages are sent upon collision
Static Rigidbody Collider Dynamic Rigidbody Collider Kinematic Rigidbody Collider Static Rigidbody Trigger Collider Dynamic Rigidbody Trigger Collider Kinematic Rigidbody Trigger Collider
Static Rigidbody Collider Y
Dynamic Rigidbody Collider Y Y Y
Kinematic Rigidbody Collider Y
Static Rigidbody Trigger Collider
Dynamic Rigidbody Trigger Collider
Kinematic Rigidbody Trigger Collider

Collision-related callback function

  • MonoBehaviour
    • OnCollisionEnter2D (Collider2D)
    • OnCollisionStay2D (Collider2D)
    • OnCollisionExit2D (Collider2D)
    • OnCollisionEnter (starts)
    • OnCollisionStay (starts)
    • OnCollisionExit (starts)

Trigger rule

  • Dynamic and Kinematic rigid body triggers and any colliding body and trigger will have callbacks.
  • A Static rigid-body collider can only have callbacks with a Dynamic or Kinematic rigid-body collider or trigger.
Trigger messages are sent upon collision
Static Rigidbody Collider Dynamic Rigidbody Collider Kinematic Rigidbody Collider Static Rigidbody Trigger Collider Dynamic Rigidbody Trigger Collider Kinematic Rigidbody Trigger Collider
Static Rigidbody Collider Y Y
Dynamic Rigidbody Collider Y Y Y
Kinematic Rigidbody Collider Y Y Y
Static Rigidbody Trigger Collider Y Y Y Y
DynamicRigidbody Trigger Collider Y Y Y Y Y Y
Kinematic Rigidbody Trigger Collider Y Y Y Y Y Y

Trigger related callback functions

  • MonoBehaviour
    • OnTriggerEnter2D (Collider2D)
    • OnTriggerStay2D (Collider2D)
    • OnTriggerExit2D (Collider2D)
    • OnTriggerEnter (starts)
    • OnTriggerStay (starts)
    • OnTriggerExit (starts)

Other details

dormancy

  • When the rigid body moves faster than a certain value, it enters a Sleeping state. To reduce performance consumption.
  • In a dormant state,
    • Does not participate in the calculation of physical systems.
    • When a collision occurs or a force acts on it, it re-participates in the calculation of the physical system.
    • If a static collider moves through a Transform and touches/leaves a Sleeping rigidbody, the rigidbody will not re-participate in the calculation and may be suspended in the air, causing unpredictable errors. You need to WakeUp by WakeUp. In theory, static colliders should not be moved, so this would not happen.