PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Detecting Collisions

Detecting Collisions

Oct 17 2019, 5:07 PM PST 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.

local part = script.Parent

local function onPartTouched(otherPart)
	print(part.Name .. " has touched " .. otherPart.Name)
end

part.Touched:Connect(onPartTouched)

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.

local part = script.Parent

local function onPartTouched(otherPart)
	-- Get the other part's parent
	local partParent = otherPart.Parent
	-- Look for a humanoid in the parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		-- Do something to the humanoid, like set its health to 0
		humanoid.Health = 0
	end
end

part.Touched:Connect(onPartTouched)

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.

local part = script.Parent

local function onPartTouchEnded(otherPart)
	print(part.Name .. " is no longer touching " .. otherPart.Name)
end

part.TouchEnded:Connect(onPartTouchEnded)
Tags:
  • collision
  • collide
  • detection
  • hit