Physics

Static Collider

Static Collider (StaticCollider) is used to create physical objects that are fixed in the scene and do not move. It is not affected by forces applied by the physics engine but can collide and trigger responses with other dynamic objects. It is commonly used to create static scene elements such as ground and walls.

Usage

  1. Select the target entity and click the Add Component button in the inspector to add the StaticCollider component.
  1. Add a collision shape to the collider. For detailed instructions on collision shapes, please refer to the Collision Shape documentation.
  1. Adjust the position, size, and other properties of the collision shape to match the scene elements.

Properties

PropertyDescription
shapesCollection of collision shapes

Methods

Method NameDescription
addShapeAdd a collision shape
removeShapeRemove a specified collision shape
clearShapesClear all collision shapes

Script Usage

Basic Configuration

// Create a static collider
const staticCollider = entity.addComponent(StaticCollider);
 
// Add a box collision shape
const boxShape = new BoxColliderShape();
boxShape.size = new Vector3(1, 1, 1);
boxShape.position = new Vector3(0, 0.5, 0);
staticCollider.addShape(boxShape);

Shape Management

// Get all shapes
const shapes = staticCollider.shapes;
 
// Add a shape
const boxShape = new BoxColliderShape();
staticCollider.addShape(boxShape);
 
// Remove a specific shape
staticCollider.removeShape(boxShape);
 
// Clear all shapes
staticCollider.clearShapes();

Trigger Settings

// Set as a trigger
const triggerShape = new BoxColliderShape();
triggerShape.isTrigger = true;
staticCollider.addShape(triggerShape);
 
// Add a trigger response script
class TriggerHandler extends Script {
  onTriggerEnter(other: Collider) {
    console.log("Trigger activated");
  }
}
entity.addComponent(TriggerHandler);

Composite Shapes

// Create an L-shaped wall
function createLWall() {
  const entity = new Entity();
  const collider = entity.addComponent(StaticCollider);
  
  // Main wall
  const wall1 = new BoxColliderShape();
  wall1.size = new Vector3(5, 3, 0.5);
  collider.addShape(wall1);
  
  // Side wall
  const wall2 = new BoxColliderShape();
  wall2.size = new Vector3(0.5, 3, 3);
  wall2.position = new Vector3(2.25, 0, 1.25);
  collider.addShape(wall2);
  
  return entity;
}

Was this page helpful?