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

How would I raycast downward at a 45 degree angle?

Asked by 3 years ago

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

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.' Ziffixture 6913 — 3y
0
Hello thank you for the post, this also worked when I put it on the X and not the Y axis. xFinatik 2 — 3y

1 answer

Log in to vote
0
Answered by
Necro_las 412 Moderation Voter
3 years ago
Edited 3 years ago
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
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. Necro_las 412 — 3y
0
Oh Alright nice to know, Thank you for the answer it works as intended. xFinatik 2 — 3y
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? xFinatik 2 — 3y
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. Necro_las 412 — 3y
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 Necro_las 412 — 3y
Ad

Answer this question