New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Still have questions? Join our Discord server and get real time help.
Log in to vote

Attempting to make an angled raycast but not working as intended?

Asked by 1 year ago

Basically, I'm trying to send three raycasts out from the character, each at a different angle. As you can see in this picture, one ray is supposed to go straight out which I can do with ease, but the outside rays are supposed to be angled and sometimes turn out wrong. You can see what I mean by turn out wrong in this gif. The third and fourth times the beams appear is what it's intended to look like, and all the other times are errors.

This is my code

function rotatedVector (vector , theta)
    local x = vector.X * math.cos(theta) - vector.Z * math.sin(theta)
    local z = vector.X * math.sin(theta) + vector.Z * math.cos(theta)
    return , vector.Y , z)

for i = 1,3 do
    local theta = math.rad(30) --angle in radians
    local lookVector1 = char.HumanoidRootPart.CFrame.lookVector * 5
    local lookVector2 = rotatedVector(lookVector1 , theta)
    local lookVector3 = rotatedVector(lookVector1 , -theta)

    local rayEnd
    if i == 1 then
        rayEnd = lookVector1
    elseif i == 2 then
        rayEnd = lookVector2
    elseif i == 3 then
        rayEnd = lookVector3
    local ray =, rayEnd) -- I've also tried using "char.HumanoidRootPart.CFrame.Position" but as expected nothing changes
    local part, position = workspace:FindPartOnRay(ray, char, false, true)

    local beam ="Part", workspace)
    beam.BrickColor ="Bright red")
    beam.FormFactor = "Custom"
    beam.Material = "Neon"
    beam.Transparency = 0
    beam.Anchored = true
    beam.Locked = true
    beam.CanCollide = false

    local distance = maxDistance
    beam.Size =, 0.3, distance)
    beam.CFrame =, position) *, 0, -distance / 2)
    game:GetService("Debris"):AddItem(beam, 1)

Any help would be appreciated.

Can you try removing the scalar of 5 that's multiplied with the look vector of the humanoid root part? RayCurse 1498 — 1y
I removed it and tried messing around with it but the results were the same. Draebrewop 114 — 1y
It works when you're standing still right? Gif makes it look like it's a Root Position issue. xPolarium 1378 — 1y
When the character turns I mean. xPolarium 1378 — 1y
View all comments (3 more)
It works occasionally when standing still, but seems to work the same as when walking. I've noticed that it depends on where the character is facing. For example, if I cast the rays at one point without ever moving, and then I cast the rays, the result will be the same as the previous cast. Draebrewop 114 — 1y
5 upvotes holy jesus User#19524 150 — 1y
Sorry Draebrewop 114 — 1y

1 answer

Log in to vote
Answered by
RayCurse 1498 Moderation Voter
1 year ago
Edited 1 year ago

The probelem I've identified that's most likely causing the issue is the lookAt parameter you are using to construct the CFrame for your beams. You are using the position of the humanoid root part and the position of the ray intersection which is the wrong way to do this. You should be using the lookVector instead of the ray intersection point. The positioning of the part should now look something like this:

local pos = char.HumanoidRootPart.CFrame.Position
local lookAt = char.HumanoidRootPart.Position + rayEnd
local offset =, 0, -distance / 2)
beam.CFrame = , lookAt) * offset

I've tested specifically using the code you posted in your question and I encountered the same issue. Upon implementing the fix, the problems went away.

You're right. Works perfectly. Thanks a lot! Draebrewop 114 — 1y
No problem RayCurse 1498 — 1y

Answer this question