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

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

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

Answer this question