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 scripting.

When considering collision handling, note the following:

  • Collisions in Roblox can cause objects to move around. If that’s not desirable based on 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, but the colliding objects’ momentum won’t be affected.
  • To hide a part that detects collisions, like a region in front of a door which detects players entering a building, set its BasePart/Transparency|Transparency to 1.
  • 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

Roblox will fire the BasePart/Touched|Touched event when parts touch or intersect, even if they are set to not physically collide (BasePart/CanCollide|CanCollide set to false). This is useful for triggering events in a game such as opening trap doors, saving progress through an obstacle course with checkpoints, or changing the skybox of an area when you walk through different parts of the map.

Part-Triggered Collisions

Any part can be used to trigger BasePart/Touched|Touched events. To use this event in your game, a function must be subscribed to it using Connect(). The Script below, when placed inside a part, shows how the BasePart/Touched|Touched event can be used 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