How do i make a door open with raycast

how would i make a door open when a player gets close to it kind of like how doors does it

3 Likes

I don’t know about raycasting but you can try hitbox using a part. Or if you still want to do raycast, you can do a raycasy in the middle of the door (not the part where the part opens and closes) and do a ray cast to nearby players and determine if they are nearby or not to open and close it.

I am not sure but you might want to use while true do or heartbeat

1 Like

do you know which one is better for this kind of thing?

1 Like

I guess a raycasting can give accurate informations. And it’s better in multiple ways you’ll just have to make sure to use it correctly.

So I’d recommend trying raycast hitbox as it can unlock multiple features for you such as obstacles in front of players and the hit position and much more.

1 Like

raycasting can overcomplicate this process as well as can burden the server. u should use magnitude in this cases to check if player is near the door e.g

local distanceFromDoor = (plrRootPart.Position - Door.Position).Magnitude
--if plr is in radius of 3 studs from the door then
if distanceFromDoor <= 3 then
--open door
2 Likes

Most of these doors just have a detector part that listens to touched events. The detector part has to be anchored and its CanCollide set to false, but CanTouch to true.

local RootsTouching = {};
Detector.Touched:Connect(function(Part)
    if (Part.Name~="HumanoidRootPart" or table.find(RootsTouching, Part)) then return; end
    local Char = Part.Parent;
    local Player = Players:GetPlayerFromCharacter(Char);
    -- Add an optional player filter here
    table.insert(RootsTouching, Part);
    if (#RootsTouching == 1) then
        -- Open the door
    end
end)

Detector.TouchEnded:Connect(function(Part)
    if (not (Part.Name=="HumanoidRootPart" and table.find(RootsTouching, Part))) then return; end
    table.remove(RootsTouching, table.find(RootsTouching, Part));
    if (#RootsTouching==0) then
        -- close the door
    end
end)
1 Like

i dont want to use .Touched because i want the door to open based on their distance from the door

1 Like

You could use a large invisible cylinder or sphere as your detector with a radius of your distance. The problem with using distance alone is that you’ll probably have a lot of doors, with each door looping through all characters individually each frame. Using Touched will have almost no performance impact as it’s done whether you want it or not by the physics engine. Using a cylinder also allows you to set precise borders so that characters somewhere above or below the door can’t activate it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.