CollisionEnter

Constructor:

CollisionEnter()

Description

Triggered when the object starts a collision.

Parameters

Name Type Description
RWObject The object is hit

Examples

Create a part p1 and add a collision event to it, and then the event will be triggered by a collision.

local function EnterEvent(res) -- define a method of starting a collision
        print("The event that is triggered when a collision starts>>>>>".." Who hit me:" ..res.Name)
end
local p1 = RWObject.Create("Part") -- create a part
p1.CollisionEnter:Connect(EnterEvent) -- the event that a collision starts

Insert two modules, and create a physical constraint for one module. When the player touches another module, disable the created physical constraint.

local temp = RWObject.Create("Part") -- create a part and assign it to the variable temp
local temp1 = RWObject.Create("Part") -- create a part and assign it to the variable temp1
temp.Position = Vector3.New(2,2,2) -- set the coordinates of temp
temp1.Position = Vector3.New(3,2,3)
temp1.Anchored = true -- fix temp1
temp:CreateJoint(temp1,JOINT_TYPE.FixedJoint) -- create a physical constraint for temp. The constraint type is Fixed
function DelAllJoint() -- define a method of deleting physical constraint
    temp1:DelAllJoint() -- remove all physical constraints from temp1
    temp1.Color = Vector3.New(255,0,0) -- set the color of temp1 to (255,0,0), that is red
end
temp1.CollisionEnter:Connect(DelAllJoint) -- when temp1 is touched, execute the method of deleting physical constraints

It is required to enable collision callback for collisions with non-human objects.
The following script can change the part color after the part hits other objects.

local temp = RWObject.Create("Part") -- create an instance temp of the part
temp.Position = Vector3.New(0,5,0)
temp.IsCollisionCallBack = true
temp.Parent = WorkSpace
temp.CollisionEnter:Connect(function(hit)
    temp.Color = Vector3.New(255,0,0) -- the part turns red after collision
end)