How can I use raycasting?

Im new to raycasting, and i viewed a few dev forums and they didnt seem to work for me.

local From = workspace:WaitForChild("From")
local To = workspace:WaitForChild("To")

local Direction = From.Position - To.Position

local Params = RaycastParams.new()

local Result = workspace:Raycast(From.Position, Direction)

if Result and Result.Instance then
    Result.Instance.Color  =Color3.new(1,0,0)
end

This i have tried yet it didn’t work

1 Like
local From = workspace:WaitForChild("From")
local To = workspace:WaitForChild("To")

local Direction = From.Position - To.Position

local Params = RaycastParams.new()

local Result = workspace:Raycast(From.Position, Direction)

if Result then
	Result.Instance.Color  = Color3.FromRGB(1,0,0)
else
	warn("No raycast result!")
end

Try this, also check anything in output

local RaycastParameters = RaycastParams.new()
RaycastParameters.IgnoreWater = true
RaycastParameters.RespectCanCollide = false
RaycastParameters.FilterType = Enum.RaycastFilterType.Exclude

local Raycast = workspace:Raycast(game.Workspace.Start.Position, game.Workspace.Start.CFrame.LookVector * 300, RaycastParameters) 

if Raycast and Raycast.Instance then 
   Raycast.Instance.Color = Color3.new(0,1,0) 
end

no raycast result

character limit charter limit

this doesnt have a direction when you raycast it

game.Workspace.Start.CFrame.LookVector * 300

What happens if you place a part between both parts?

yeah well either way it doesnt work

still gives the warning “no raycast result”

For me it does, did you make sure to rotate the parts correctly?

Wait so its based on orientation?

Yes, it depends on the way the part is facing. I made it use a part for the position and direction for easier testing on my side.

1 Like

What about doing this with characters?
Like if the characters leg touches another charcaters leg

It should return the characters leg.

In their case, it would not depend on how the part faces though

its not on the parts face its what the part touches but that part is the characters limb

Might you know?

lets say you touch a object and you are using the .touched function

how do you raycast if it was a players leg? or the object

.Touched returns the p2 that touched p1 so there’s really no need to check via raycast if it was a players leg or a specific object.

It depends why and what you are raycasting too. You have the hit in the .Touched function. In this case, then face might matter

i think the the direction should be To.Position - From.Position because the difference of two points has the target or going to position as the minuend while the starting or coming from position is the subtrahend

local From = workspace:WaitForChild("From")
local To = workspace:WaitForChild("To")

local Direction = To.Position - From.Position

local Params = RaycastParams.new()

local Result = workspace:Raycast(From.Position, Direction)

if Result and Result.Instance then
    Result.Instance.Color = Color3.new(1,0,0)
end