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

How would I raycast downward at a 45 degree angle?

Hello, I am fairly new to lua and roblox scripting, How would I go about casting a ray downward at a 45 degree angle from the torso/humanoidrootpart? I have browsed a few other forum posts and none of the way's have worked for me. Here's what I was able to do but downwards was not successful neither was a 45 degree angle. Any help will be appreciated.

local player = game.Players.LocalPlayer
local char = player.Character
local torso = char.HumanoidRootPart

local origin = torso.Position
local direction = torso.CFrame.LookVector * 100

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {char}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local raycastResult = workspace:Raycast(origin, CFrame.Angles(0,math.rad(90),0).lookVector * direction, raycastParams)	
	
if raycastResult then
	print(raycastResult.Instance)
else 
	print("hit nothing")
end

If I have worded this wrong this is what I want to happen

Drawn Example

2 comments

0

You're Raycasting ninety degrees upward. As the rules of a Cartesian coordinate illustrate, a negative Y-position will reach downward, therefore you must reformat your Vector like so: 'CFrame.Angles(0, math.rad(-45), 0).LookVector * Scale.'

0

Hello thank you for the post, this also worked when I put it on the X and not the Y axis.

1 answer

✓ Accepted answer
local distance = 10

local torso = char.HumanoidRootPart
local Diagonal_Front_Down = torso.Position + torso.CFrame.LookVector * distance - Vector3.new(0,distance,0)

local Origin = -- set your stating point here (Vector3)
local EndPoint = Diagonal_Front_Down

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {char}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local raycastResult = workspace:Raycast(Origin, EndPoint - Origin, raycastParams)

if raycastResult then
	print(
		raycastResult.Position,
		raycastResult.Instance,
		raycastResult.Material,
		raycastResult.Normal
	)
	-- mor info in https://developer.roblox.com/en-us/api-reference/function/WorldRoot/Raycast
end

5 comments

0

Note: the actual distance of the ray will not be exactelly 10 studs, it will be (distance + distance / 2.25), 14.4 in the example.

0

Oh Alright nice to know, Thank you for the answer it works as intended.

0

This also leads me to another question that you may be able to answer, what is the possibility of creating multiple rays with this method just on a different side of the origin?

0

to create another ray you have to write raycastResult = workspace:ray... with another valua as EndPoint. CFrame.LookVector can be switched to CFrame.UpVector or CFrame.RightVector to get other orientations then the front. Or insted of adding just Vector3.new(0,-10,0), you can use like (10,-10,0) to raycast in diagonal.

0

Or if you want to change the Origin point, for exemple I want the ray to begin 5 studs in front of the char, not IN it, then Origin = Torso.Position + Torso.CFrame.LookVector * 5

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