Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

2 comments

1

Would the PathfindingService be what you're looking for?

0

Ah, this is something I didn't think of. Thanks a lot!

1 answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.