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

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

Asked by 5 years 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 Vector3.new(x , vector.Y , z)
end

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
    end
    local ray = Ray.new(char.HumanoidRootPart.Position, 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 = Instance.new("Part", workspace)
    beam.BrickColor = BrickColor.new("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 = Vector3.new(0.3, 0.3, distance)
    beam.CFrame = CFrame.new(char.HumanoidRootPart.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
    game:GetService("Debris"):AddItem(beam, 1)
end

Any help would be appreciated.

0
Can you try removing the scalar of 5 that's multiplied with the look vector of the humanoid root part? RayCurse 1518 — 5y
0
I removed it and tried messing around with it but the results were the same. Draebrewop 114 — 5y
0
It works when you're standing still right? Gif makes it look like it's a Root Position issue. xPolarium 1388 — 5y
0
When the character turns I mean. xPolarium 1388 — 5y
View all comments (3 more)
0
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 — 5y
0
5 upvotes holy jesus User#19524 175 — 5y
0
Sorry Draebrewop 114 — 5y

1 answer

Log in to vote
4
Answered by
RayCurse 1518 Moderation Voter
5 years ago
Edited 5 years 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 = CFrame.new(0, 0, -distance / 2)
beam.CFrame = CFrame.new(pos , 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.

0
You're right. Works perfectly. Thanks a lot! Draebrewop 114 — 5y
0
No problem RayCurse 1518 — 5y
Ad

Answer this question