Physics
The Physics class is essential for managing global physical simulations in scenes, such as gravity, collisions, and interactions between GameObjects. It also allows control over settings like gravity and layer collisions.
Properties
Control the global gravity in the physics simulation, defaulting to Earth's gravity.
Physics.gravity = Vector3.new(0, -9.81, 0)
Define the minimum distance between colliders for them to be considered in contact.
Physics.defaultContactOffset = 0.01
Set the velocity threshold below which GameObjects are considered static or immobile.
Physics.sleepThreshold = 0.01
Determine whether physics queries interact with trigger colliders.
Physics.queriesHitTriggers = false
Decide if physics queries should detect hits on collider backfaces.
Physics.queriesHitBackfaces = false
Specify the minimum collision velocity for an object to bounce.
Physics.bounceThreshold = 0.1
Methods
Refer to Unity's documentation for information on maximum depenetration velocity.
local maxDepenetrationVelocity = Physics.defaultMaxDepenetrationVelocity
print("Default max depenetration velocity: " .. maxDepenetrationVelocity)
Refer to Unity's documentation for information on solver iterations.
Physics.defaultSolverIterations = 10
Refer to Unity's documentation for information on solver velocity iterations.
Physics.defaultSolverVelocityIterations = 10
Refer to Unity's documentation for information on maximum angular speed.
Refer to Unity's documentation for information on improved patch friction.
Physics.improvedPatchFriction = true
Refer to Unity's documentation for information on collision callbacks invocation.
Physics.invokeCollisionCallbacks = true
Refer to Unity's documentation for information on automatic transforms synchronization.
Refer to Unity's documentation for information on reusing collision callbacks.
Physics.reuseCollisionCallbacks = true
Refer to Unity's documentation for information on inter-collision distance.
Physics.interCollisionDistance = 0.1
Refer to Unity's documentation for information on inter-collision stiffness.
Physics.interCollisionStiffness = 0.5
Refer to Unity's documentation for information on inter-collision settings toggle.
Physics.interCollisionSettingsToggle = true
Refer to Unity's documentation for information on cloth gravity.
Physics.clothGravity = Vector3.new(0, -9.81, 0)
Refer to Unity's documentation for information on the Ignore Raycast layer.
local ignoreRaycastLayer = Physics.IgnoreRaycastLayer
print("Ignore Raycast layer: " .. ignoreRaycastLayer)
Refer to Unity's documentation for information on default raycast layers.
Refer to Unity's documentation for information on all layers.
local allLayers = Physics.AllLayers
print("All layers: " .. allLayers)
Trace a path in 3D space to detect colliders, useful for implementing bullet paths or line-of-sight features.
local ray = Ray.new(Vector3.new(0, 0, 0), Vector3.new(0, 0, 1))
local maxDistance = 10
local layerMask = 0
local hit, hitDetails = Physics.Raycast(ray, maxDistance, layerMask)
print("Hit: " .. tostring(hit))
Parameters
ray
RayDefines the origin and direction of the ray.
maxDistance
The maximum distance to check for collisions.
layerMask
LayerMask to selectively ignore colliders.
Returns
Returns true if any collider is hit, with collision details.
Find all colliders touching or inside an imaginary sphere, useful for area damage or proximity checks.
local position = Vector3.new(0, 0, 0)
local radius = 5
local layerMask = 0
local colliders = Physics.OverlapSphere(position, radius, layerMask)
print("Colliders count: " .. #colliders)
Parameters
position
Vector3Sphere center.
radius
Sphere radius.
layerMask
LayerMask to selectively ignore colliders.
Returns
Array of colliders within the sphere.
Prevent physics engine from processing collisions between two specified colliders.
local collider1 = GameObject.Find("Collider1"):GetComponent(Collider)
local collider2 = GameObject.Find("Collider2"):GetComponent(Collider)
Physics.IgnoreCollision(collider1, collider2, true)
Make physics engine ignore all collisions between colliders in two specified layers.
local layer1 = 8
local layer2 = 9
Physics.IgnoreLayerCollision(layer1, layer2, true)
Parameters
layer1
First layer ID.
layer2
Second layer ID.
ignore
Whether to ignore collisions.
Returns
Does not return a value.
Check if collisions between any colliders in two layers are ignored.
local layer1 = 8
local layer2 = 9
local isIgnored = Physics.GetIgnoreLayerCollision(layer1, layer2)
print("Layer collision ignored: " .. tostring(isIgnored))
Parameters
layer1
First layer ID.
layer2
Second layer ID.
Returns
True if collisions are ignored, false otherwise.
Check if collisions between two specific colliders are ignored.
local collider1 = GameObject.Find("Collider1"):GetComponent(Collider)
local collider2 = GameObject.Find("Collider2"):GetComponent(Collider)
local isIgnored = Physics.GetIgnoreCollision(collider1, collider2)
print("Collision ignored: " .. tostring(isIgnored))
Advance physics simulation by a specified time, useful for 'fast-forwarding' physics.
Physics.Simulate(1)
Parameters
step
Time to advance simulation by, in seconds.
Returns
Does not return a value.
Synchronize transform data used in physics simulation with GameObjects' actual transform data.
Physics.SyncTransforms()
Returns
Does not return a value.
Calculate the closest point to a given position on a collider's surface.
local point = Vector3.new(0, 0, 0)
local collider = GameObject.Find("Collider"):GetComponent(Collider)
local position = Vector3.new(0, 0, 0)
local rotation = Quaternion.new(0, 0, 0, 1)
local closestPoint = Physics.ClosestPoint(point, collider, position, rotation)
print("Closest point: " .. tostring(closestPoint))
Parameters
point
Vector3Position to check against.
collider
ColliderCollider to check.
position
Vector3Collider's position.
rotation
QuaternionCollider's rotation.
Returns
Closest point on the collider.
Force a rebuild of the tree for broad-phase collision detection after static collider movements.
Physics.RebuildBroadphaseRegions()
Parameters
worldBounds
BoundsNew world boundaries for the physics simulation.
subdivisions
Number of subdivisions in the broadphase tree.
Returns
Does not return a value.