How would I go about finding if two objects have a part between them?

Hello!

I’m currently creating an interaction handler which uses .Magnitude to determine if a player’s character is within a certain radius of an interact-able object. The problem is that the interaction prompt appears even when there is an object in between the player and the object. I assume I would have to use raycasting for this, however I have zero experience with creating my own rays, the most experience I have is mouse.Hit.

Another thing is that this tarp is a mesh so I don’t believe I could use :GetTouchingParts() as it doesn’t seem efficient, and I don’t believe it could check as accurately as raycasting would.

What calculates the distance between the character and the object (works fine).

runService.RenderStepped:Connect(function()
    local closestDistance = math.huge
    local closestObject = nil
    for i,interactionObject in pairs(interactableObjects) do
        if interactionObject:IsA('Model') then
            interactionObject = interactionObject.PrimaryPart
        end
        if (humanoidRootPart.Position - interactionObject.Position).Magnitude < minimumInteractionDistance then
            if (humanoidRootPart.Position - interactionObject.Position).Magnitude < closestDistance then
                closestDistance = (humanoidRootPart.Position - interactionObject.Position).Magnitude
                closestObject = interactionObject
            end
        end
    end
    if not closestObject then
        template.Parent = nil
        template.Adornee = nil
    elseif closestObject ~= openInteraction then
        promptInteraction(closestObject)
    end
end)

Here’s the market stall:

(Extremely) rough idea of where the player should be able to interact with the object.

Uh why don’t u just put a transparent part in the area u want them to be at to interact

inside the transparent part just add a script and write this

 script.Parent.Touch:Connect(function()

--blah blah blah
end)
1 Like

.Touched and (especially) .TouchEnded aren’t very accurate so the user would end up with the interact stuck on their screen or it wouldn’t appear at all. Plus I have an entire system created and I don’t really want to create a new one using this method unfortunately. I’m only really looking to refine the current system.

I also went about creating a system like this awhile ago and it all in all didn’t work very well, especially with multiple interact popups.

If I understand this correctly you want to only show something when the player is at a given angle right? Assuming this is the case you can use the dot product of 2 vectors and get the given angle between 2 parts with this function

local function GetAngleBetween(part1, part2)
    return math.acos(part.CFrame.LookVector:Dot((part1.Position - part2.Position).Unit))
end
1 Like

Nah, I mean if there’s an obstruction between the player and the interactable object, the interact prompt shouldn’t appear.

Made this diagram, excuse my crappy trackpad drawing skills lol.

You can use ray casting for this. Cast a ray from the NPC’s humanoid root part inside the market stall and check if something hits the ray, if it does, there is a part between the market stall.

Demonstration:

local MAX_DISTANCE = 35 
local ray = workspace:RayCast(npc.HumanoidRootPart.Position, npc.HumanoidRootPart.CFrame.LookVector * MAX_DISTANCE)

-- If ray is not nil, there is a part in between
if (ray ~= nil) then

end
1 Like

You could use Proximity Prompt to let the player hold down a key to enter the shop instead of doing it by touch

2 Likes