Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Physics2D

Provides utilities and global settings to manage and simulate 2D physics interactions, such as collision detection and raycasting.
Read time 5 minutesLast updated 4 days ago

Definition

  • Type: Class
  • Namespace: UnityEngine
  • Assembly: UnityEngine.Physics2DModule
public class Physics2D

Remarks

The Physics2D class provides tools and global settings for simulating 2D physics interactions, such as collision detection and raycasting. It is crucial for integrating realistic physics behaviors in 2D games, such as platformers and puzzle games.
To use Physics2D, set up your GameObjects with Collider2D-based, and Rigidbody2D components if required, and ensure proper configuration of layers and tags for collision detection and raycasting. Be mindful of potential performance impacts when using physics in complex scenes. Common issues may arise from incorrect setup of Collider2D-based or Rigidbody2D components, so understanding the order of physics updates and force application is important for troubleshooting.

Static Fields

Value

Description

AllLayersLayer mask constant that includes all layers.
DefaultRaycastLayersLayer mask constant that includes all layers participating in raycasts by default.
IgnoreRaycastLayerLayer mask constant for the default layer that ignores raycasts.
MaxPolygonShapeVerticesThe maximum number of vertices allowed per primitive polygon shape type (PhysicsShapeType2D.Polygon). (Read Only)

Static Properties

Property

Description

angularSleepToleranceA Rigidbody cannot sleep if its angular velocity is above this tolerance threshold.
baumgarteScaleThe scale factor that controls how fast overlaps are resolved.
baumgarteTOIScaleThe scale factor that controls how fast TOI overlaps are resolved.
bounceThresholdAny collisions with a relative linear velocity below this threshold will be treated as inelastic so no bounce will occur.
callbacksOnDisableUse this to control whether or not the appropriate OnCollisionExit2D or OnTriggerExit2D callbacks should be called when a Collider2D is disabled.
contactThresholdA threshold below which a contact is automatically disabled.
defaultContactOffsetThe default contact offset of the newly created Colliders.
defaultPhysicsSceneThe PhysicsScene2D automatically created when Unity starts.
gravityAcceleration due to gravity.
jobOptionsA set of options that control how physics operates when using the job system to multithread the physics simulation.
linearSleepToleranceA rigid-body cannot sleep if its linear velocity is above this tolerance.
maxAngularCorrectionThe maximum angular position correction used when solving constraints. This helps to prevent overshoot.
maxLinearCorrectionThe maximum linear position correction used when solving constraints. This helps to prevent overshoot.
maxRotationSpeedThe maximum angular speed of a rigid-body per physics update. Increasing this can cause numerical problems.
maxSubStepCountThe maximum number of simulation sub-steps allowed per-frame when simulation sub-stepping is enabled.
maxTranslationSpeedThe maximum linear speed of a rigid-body per physics update. Increasing this can cause numerical problems.
minSubStepFPSThe minimum FPS allowed for a simulation step before sub-stepping will be used.
positionIterationsThe number of iterations of the physics solver when considering objects' positions.
queriesHitTriggersDo raycasts detect Colliders configured as triggers?
queriesStartInCollidersSet the raycasts or linecasts that start inside Colliders to detect or not detect those Colliders.
reuseCollisionCallbacksDetermines whether the garbage collector should reuse only a single instance of a Collision2D type for all collision callbacks.
simulationLayersThe Rigidbody2D and Collider2D layers to simulate.
simulationModeControls when Unity executes the 2D physics simulation.
timeToSleepThe time in seconds that a rigid-body must be still before it will go to sleep.
useSubStepContactsWhether to calculate contacts for all simulation sub-steps or only the first sub-step.
useSubSteppingWhether to use simulation sub-stepping during a simulation step.
velocityIterationsThe number of iterations of the physics solver when considering objects' velocities.

Static Methods

Method

Description

BoxCastCasts a box against Colliders in the Scene, returning the first Collider to contact with it.
BoxCastAllCasts a box against Colliders in the Scene, returning all Colliders that contact with it.
CapsuleCastCasts a capsule against Colliders in the Scene, returning the first Collider to contact with it.
CapsuleCastAllCasts a capsule against Colliders in the Scene, returning all Colliders that contact with it.
CircleCastCasts a circle against Colliders in the Scene, returning the first Collider to contact with it.
CircleCastAllCasts a circle against Colliders in the Scene, returning all Colliders that contact with it.
ClosestPointReturns a point on the perimeter of the
Collider
that is closest to the specified
position
.
DistanceCalculates the minimum distance between two Colliders.
GetContactCollidersRetrieves all colliders in contact with this Collider, with the results filtered by the
contactFilter
.
GetContactsRetrieves all contact points in for contacts between with the
collider1
and
collider2
, with the results filtered by the
contactFilter2D
.
GetIgnoreCollisionChecks whether the collision detection system will ignore all collisions/triggers between
collider1
and
collider2
or not.
GetIgnoreLayerCollisionChecks whether collisions between the specified layers be ignored or not.
GetLayerCollisionMaskGet the collision layer mask that indicates which layer(s) the specified layer can collide with.
GetRayIntersectionCast a 3D ray against the 2D Colliders in the Scene.
GetRayIntersectionAllCast a 3D ray against the 2D Colliders in the Scene.
IgnoreCollisionMakes the collision detection system ignore all collisions/triggers between
collider1
and
collider2
.
IgnoreLayerCollisionChoose whether to detect or ignore collisions between a specified pair of layers.
IsTouchingChecks whether the passed Colliders are in contact or not.
IsTouchingLayersChecks whether the
Collider
is touching any Colliders on the specified
layerMask
or not.
LinecastCasts a line segment against Colliders in the Scene.
LinecastAllCasts a line against Colliders in the Scene.
OverlapAreaFinds Colliders that intersect a rectangular aream returning the first it encounters.
OverlapAreaAllGet a list of all Colliders that fall within a rectangular area.
OverlapBoxChecks if a Collider falls within a box area.
OverlapBoxAllGet a list of all Colliders that fall within a box area.
OverlapCapsuleChecks a capsule against Colliders in the scene, returning the first it encounters.
OverlapCapsuleAllGet a list of all Colliders that fall within a capsule area.
OverlapCircleChecks if a Collider falls within a circular area.
OverlapCircleAllGet a list of all Colliders that fall within a circular area.
OverlapColliderGets a list of all Colliders that overlap the given
Collider
.
OverlapPointChecks a point against Colliders in the scene, returning the first Collider it encounters.
OverlapPointAllGet a list of all Colliders that overlap a point in space.
RaycastCasts a ray against Colliders in the Scene.
RaycastAllCasts a ray against Colliders in the Scene, returning all Colliders that contact with it.
SetLayerCollisionMaskSet the collision layer mask that indicates which layer(s) the specified layer can collide with.
SimulateSimulate physics in the default physics scene.
SyncTransformsSynchronizes.