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