• Studio

  • Studio API

  • Bots

  • Web API

  • Designer Resources

  • Host Resources

  • 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
    

    clothGravity

    DataType.Vector3

    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.

    AllLayers

    number

    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
    Ray

    Defines the origin and direction of the ray.

    maxDistance

    number

    The maximum distance to check for collisions.

    layerMask

    number

    LayerMask to selectively ignore colliders.

    Returns

    RaycastHit)

    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
    Vector3

    Sphere center.

    radius

    number

    Sphere radius.

    layerMask

    number

    LayerMask to selectively ignore colliders.

    Returns

    Collider[]

    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)
    

    Parameters

    collider1
    Collider

    First collider.

    collider2
    Collider

    Second collider.

    ignore

    boolean

    Whether to ignore collisions.

    Returns

    void

    Does not return a value.

    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

    number

    First layer ID.

    layer2

    number

    Second layer ID.

    ignore

    boolean

    Whether to ignore collisions.

    Returns

    void

    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

    number

    First layer ID.

    layer2

    number

    Second layer ID.

    Returns

    boolean

    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))
    

    Parameters

    collider1
    Collider

    First collider.

    collider2
    Collider

    Second collider.

    Returns

    boolean

    True if collisions are ignored, false otherwise.

    Advance physics simulation by a specified time, useful for 'fast-forwarding' physics.

    Physics.Simulate(1)
    

    Parameters

    step

    number

    Time to advance simulation by, in seconds.

    Returns

    void

    Does not return a value.

    Synchronize transform data used in physics simulation with GameObjects' actual transform data.

    Physics.SyncTransforms()
    

    Returns

    void

    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
    Vector3

    Position to check against.

    collider
    Collider

    Collider to check.

    position
    Vector3

    Collider's position.

    rotation
    Quaternion

    Collider's rotation.

    Returns

    Vector3

    Closest point on the collider.

    Force a rebuild of the tree for broad-phase collision detection after static collider movements.

    Physics.RebuildBroadphaseRegions()
    

    Parameters

    worldBounds
    Bounds

    New world boundaries for the physics simulation.

    subdivisions

    number

    Number of subdivisions in the broadphase tree.

    Returns

    void

    Does not return a value.

    Updated about 1 month ago

    PocketWorlds Icon

    © 2024 Pocket Worlds. All rights reserved.