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

Is there a way to find the angle a ray was fired?

The object that the ray is being fired out of is constantly rotating. What I'm trying to do is to find the angle between the direction the object is facing and the direction of the ray being fired. What I tried to do is subtract the direction of the ray from the object's look vector, but that didn't really work. Here's my script:

local Camera = script.Parent
local ignorelist = {script.Parent.Parent.Parent}

while wait() do
	for i,v in pairs(game.Workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local head = v:FindFirstChild("Head")
		
		if human and head then
			if (Camera.Position - head.Position).magnitude < 40 then
				NewRay = Ray.new(Camera.Position, (head.Position - Camera.Position))
				local Angle = NewRay.Direction - Camera.CFrame.LookVector
				local Hit = workspace:FindPartOnRayWithIgnoreList(NewRay, ignorelist)
				if Hit then
					if Hit.Parent.Name == "Guard" then
						----- guard input
					else
						if Hit.Name == "Head" then
							print(Angle)
						end
					end
				end
			end
		end
	end
end

Thanks.

1 comment

0

welcome to the world of trig xd

1 answer

There's probably a better solution than mine, but what I would do, since I'm not incredible with math, is cast a ray to the part from the end of the other ray, with the exact same direction as the other ray, except reversed, so that the new ray hits the part.

You can get the surface that the ray hit by taking the third value that is returned from calling the function FindPartOnRay:

local part, position, surface = game.Workspace:FindPartOnRay(ray)
if surface then
	print(surface)
end

The surface variable will be a Vector3 value relative to the part that's constantly rotating.

For example, if you hit the right side of the part, it'll return the Vector3 value 1, 0, 0, because positive X is to the right.

Based on the value that you get, you can determine the surface that the ray hit, and thus determine the angle from the front face of the part to the face that the ray hit.

If the surface variable is 1, 0, 0, that means you hit the right face of the part. If the surface variable is -1, 0, 0, that means you hit the left face. 0, 1, 0 would be the top, 0, -1, 0 would be the bottom, 0, 0, 1 would be the front, and 0, 0, -1 would be the back.

It's either going to be 90 degrees or 180 degrees. If it's right or left, top or bottom, it'll be 90 degrees, since those faces' edges are touching the front face's edge. The only time it'll be anything other than 90 is when the face is the front face, in which case the angle will be 0, or when the face is the back face, where it'll be 180 degrees, because you'd have to rotate the part 180 degrees to get the front face to be in the previous position of the back face.

But if you want to get the angle between the surface normal of the front face and the position at which the ray hit, and not the position of the surface that the ray hit, you'll have to do some math... which, like I said, I am not very good at. I'm sure there's a solution for that scenario, but I've tried to contribute at the very least.

1 comment

0

Also, you need to put .Unit after (head.Position - Camera.Position) to get a directional value, so it looks like this: (head.Position - Camera.Position).Unit

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