Hi there!. I am currently making a swinging system, and I want to detect nearby parts / building if possible, I have tried using raycast and return the part hit, but I have to cast a bunch of rays in order for it to work like I want, so I dont think its really efficient. Is there anyway to do that other than using raycasting ?
CFrame ObjectSpace is ideal for this. It’s relatively simple, all it is is this:
Npc_CFrame:ToObjectSpace(PlayerCFrame).Z
Psuedocode:
while true do
for i,v in pairs(game:GetService("Players"):GetPlayers()) do
if Npc_CFrame:ToObjectSpace(v.CFrame).Z < 0 then
Npc.Humanoid.WalkSpeed += 1
break
end
end
task.wait(1)
end
Keep in mind this pseudocode is just an example as it isn’t meant to be used as-is, and it doesn’t account for range, Basically Detects Parts or players nearby, i did this example with an player.
2 Likes
Here’s how I would go about it.
Pseudocode:
local bestPosition
local bestPositionAngle
for _, position in (swingParts) do
if (position - humanoidRootPart).Magnitude > THRESHOLD then continue end
local angle = math.abs(getAngleBetweenVectors((position - humanoidRootPart).Unit, mouse.UnitRay))
if angle > ANGLE_THRESHOLD then continue end
local raycastResult = workspace:RayCast(humanoidRootPart, position - HumanoidRootPart, raycastParams)
if raycastResult then continue end
if bestPositionAngle then
if bestPositionAngle > angle then
bestPositionAngle = angle
bestPosition = position
end
else
bestPositionAngle = angle
bestPosition = position
end
2 Likes

