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

How could I raycast to an angle from a certain point?

Asked by 5 years ago
Edited 5 years ago

Hello I'm trying to cast three rays in total each at a different angle. The first ray (ray 2 in the picture) is easy, and the script I used to make that ray is this local ray = Ray.new(char.HumanoidRootPart.CFrame.p, (char.HumanoidRootPart.CFrame.lookVector*5))

However, I'm trying to do that, but at an angle. To explain more thoroughly, I'll draw a picture here.

I am able to draw the Ray 2, but I tried using CFrame.Angles() to draw the Ray 1 and Ray 3 but couldn't figure it out. Any help is appreciated.

1 answer

Log in to vote
1
Answered by
RayCurse 1518 Moderation Voter
5 years ago
Edited 5 years ago

You can rotate the vector on the x and z axes like so using a little bit of trigonometry (or you can just plug your values into the formula in the webpage linked):

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.new(x , Vector.Y , z)
end

From here, it's relatively simple. Simply rotate the vectors by a certain angular offset from the lookVector.

local theta = ... --angle in radians
local lookVector1 = char.HumanoidRootPart.CFrame.lookVector * 5
local lookVector2 = rotatedVector(lookVector1 , theta)
local lookVector3 = rotatedVector(lookVector1 , -theta)

--Use these vectors to perform you ray casts
local origin = char.HumanoidRootPart.CFrame.Position
local ray1 = Ray.new(origin , lookVector1)
local ray2 = Ray.new(origin , lookVector2)
local ray3 = Ray.new(origin , lookVector3)

You can read up on rays more here and you can use these raycasts with the FindPartOnRay() method of workspace.

0
ray-cast ha get it hhhahahahaha User#19524 175 — 5y
0
you're so funny hahaha RayCurse 1518 — 5y
0
ikr ha ???????? User#19524 175 — 5y
0
ha RayCurse 1518 — 5y
View all comments (10 more)
0
Sorry I'm really bad at raycasting and I'm confused on how to cast the ray using the vectors? Draebrewop 114 — 5y
0
You already demonstrated knowledge of doing it. You just did it in your question using the lookVector of the humanoid root part! RayCurse 1518 — 5y
0
I'm confused about how to use the three different vectors, previously I only used one to cast the ray. Draebrewop 114 — 5y
0
Updated code and explanation to explain rays and link a few articles from the api ref. RayCurse 1518 — 5y
0
The code works and it displays 3 rays, and normally works as intended, but occasionally the angled rays go from an angle such as 30 to 180. Draebrewop 114 — 5y
0
Can you show a gif or recording of what happens? RayCurse 1518 — 5y
0
https://gyazo.com/771a876f47c71323868d77e399891414 The third and fourth times the beams appear is what it would ideally look like, and the rest of the times are what it should not look like. Draebrewop 114 — 5y
0
I've also noticed that if I click once and a "bad" raycast happens, if I don't move my mouse (which is moving the HumanoidRootPart) the same exact raycast happens. Draebrewop 114 — 5y
0
Can you show all the code you're using to perform the ray cast and position the neon laser beams? Either post a new question (accept this one) or edit it in because I'm pretty sure that it's nothing wrong with the vector rotation. RayCurse 1518 — 5y
0
I created a new question, could you look at my code when you get the chance? https://scriptinghelpers.org/questions/68653/attempting-to-make-an-angled-raycast-but-not-working-as-intended Draebrewop 114 — 5y
Ad

Answer this question