PcoWSkbVqDnWTu_dm2ix

Detecting Collisions

Detecting Collisions

5 min

Collisions occur when virtual shapes intersect each other in the game world. In Roblox, these virtual shapes are BasePart objects. What happens when they intersect is your decision through the BasePart/Touched|Touched and BasePart/TouchEnded|TouchEnded events.

When considering collision handling, note the following:

  • Collisions in Roblox can cause objects to move around. If that’s undesirable in your use case, disable BasePart/CanCollide|CanCollide on one or both objects involved in the collision.
  • BasePart/Touched|Touched and BasePart/TouchEnded|TouchEnded events will still trigger on parts with BasePart/CanCollide|CanCollide disabled. However, you can disable BasePart/CanTouch|CanTouch on objects which should never register a collision event.
  • BasePart|BaseParts can also use custom meshes. Check out Enum/CollisionFidelity|CollisionFidelity to learn how collision detection can be fine-tuned to custom meshes.

Touched Events

Part-Triggered Collisions

The Script below, when placed inside a part, shows how the BasePart/Touched|Touched event can be connected to the custom onPartTouched function to detect collisions with another part.

Character-Triggered Collisions

To detect if a player’s character has touched a part, find the parent of the colliding part and check if it contains a Humanoid object. This script must be placed within the part that should detect collisions.

TouchEnded Events

Roblox also provides the BasePart/TouchEnded|TouchEnded event which fires when a collision ends. This is useful to detect when two intersecting objects stop touching.

Tags:
  • collision
  • collide
  • detection
  • hit