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

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

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

✓ Accepted answer

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.

14 comments

0

ray-cast ha get it hhhahahahaha

0

you're so funny hahaha

0

ikr ha ????????

0

ha

0

Sorry I'm really bad at raycasting and I'm confused on how to cast the ray using the vectors?

0

You already demonstrated knowledge of doing it. You just did it in your question using the lookVector of the humanoid root part!

0

I'm confused about how to use the three different vectors, previously I only used one to cast the ray.

0

Updated code and explanation to explain rays and link a few articles from the api ref.

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.

0

Can you show a gif or recording of what happens?

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.

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.

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.

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

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