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

How do you position a part off of the position a Raycast hit?

I'm trying to Raycast the bottom of a character and get the position of where it hit. Then use that position to position a part. It's not working very well though. How do I do this?

1 answer

✓ Accepted answer

There are two functions you'll likely want to use for this:

Ray.new(origin, direction)

and

workspace:FindPartOnRayWithIgnoreList(ray, ignorelist, false, false)

The "origin" and "direction" within the first segment are each Vector3 values (such as found in Position properties)

In the second segment, you can disregard the "false, false" section, though if you want to learn more about it you can find out here. The ignorelist is simply a table of instances for the ray to ignore.

In order to get the position of where the ray hit an object, you can use the following:

local hit, position, surface, material = workspace:FindPartOnRayWithIgnoreList(ray, ignorelist, false, false)

where the second variable given back "position" is the Vector3 you're looking for.

Combined Example

local part = script.Parent

part.Touched:Connect(function(touch)
	if touch and touch.Parent and game:GetService("Players"):GetPlayerFromCharacter(touch.Parent) then
		local root = touch.Parent:WaitForChild("HumanoidRootPart")
		local ray = Ray.new(root.Position, Vector3.new(0, 0, 0))
		local hit, position, surface, material = workspace:FindPartOnRayWithIgnoreList(ray, touch.Parent:GetDescendants(), false, false)

		part.Position = position
	end
end)

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