Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Is there a way to check for possible collisions without checking anything in the workspace?

Asked by
Wutras 294 Moderation Voter
8 years ago

I've been making a character, which is supposed to walk around a place and give gifts to other, but this is nothing important. My problem is that I'm needing a method to find possible collisions in a specific area to prevent this character walking against walls, etc., thus I was wondering if someone here could give me a soon answer before I have to look through any article about collision detection in ROBLOX wikia.

1
Would the PathfindingService be what you're looking for? BlueTaslem 18071 — 8y
0
Ah, this is something I didn't think of. Thanks a lot! Wutras 294 — 8y

1 answer

Log in to vote
0
Answered by
Wutras 294 Moderation Voter
8 years ago

Alright, found a useful article on ROBLOX wikia about it. The function FindPartOnRay(ray, ignoreDescendantsInstance); This function searches for parts that would be in the way of the ray that must be defined with a variable before like this:

local ray = Ray.new(Vector3.new(5, 10, 13),
Vector3.new(0, 5, 0)
)
print(workspace:FindPartOnRay(ray)) -- The second argument is optional and not needed for my problem.

Then you should check whether the function turns nil. If it does then there is not gonna be anything between the beginning of the ray and the end. So to solve my problem this would be alright:

actions = false
local ray1 = Ray.new(script.Parent.Torso.Position, Vector3.new(20, 0, 0))
local ray2 = Ray.new(script.Parent.Torso.Position, Vector3.new(0, 0, 20))
local ray3 = Ray.new(script.Parent.Torso.Position, -Vector3.new(20, 0, 0))
local ray4 = Ray.new(script.Parent.Torso.Position, -Vector3.new(0, 0, 20))
while actions == false and wait(5, 15) do
    if workspace:FindPartOnRay(ray1) == nil and workspace:FindPartOnRay(ray2) == nil and workspace:FindPartOnRay(ray3) == nil and workspace:FindPartOnRay(ray4) == nil then
        script.Parent.Humanoid:MoveTo(Vector3.new(math.random(0, 20), 0, math.random(0, 20)))
    end
    wait()
end
Ad

Answer this question