Raycasting in degrees?

Hey! So I’ve been trying to figure out how I can cast a ray, with a direction given in degrees. I’ve noticed that for it’s direction parameter it requires a Vector3 argument, which disables the possibility of simply creating a new CFrame and getting/using angles from that. Apart from that method, I’m quite unsure of how I’d go about giving a specific direction for the ray. Say I wanted it at a 45 degree angle. I’m not really sure how I’d go about doing that…any help would be highly appreciated.

Thank you!

1 Like

I’ve decided that parsing English is nearly impossible and wholly unsuited for communication due to its inherent tendency for ambiguity. So, here is some beautiful Lua which may help:

local function getRelative(originCF, axis, angle)
  angle = math.rad(angle)
  return originCF * CFrame.fromAxisAngle(axis, angle)
end

local function makeRay(origin, dir)
  local p = Instance.new 'Part'
  p.Size = Vector3.new(0.2, 0.2, 100)
  p.CFrame = CFrame.new(origin + dir/2, origin + dir)
  p.Anchored = true
  p.Parent = workspace
end

local foo = CFrame.new()
while wait(1) do
  makeRay(foo.p, foo.LookVector)
  foo = getRelative(foo, Vector3.new(0, 1, 0), 15)
end

Is this what you are looking for?

You could construct a new CFrame from angles in degrees, and then get that lookvector of that

local X_DEGREES = 0      --pitches the vector like a plane
local Y_DEGREES = 45     --turns the vector like a car
local Z_DEGREES = 0      --rolls the vector like a dog in grass

local vec = CFrame.Angles(math.rad(X_DEGREES), math.rad(Y_DEGREES), math.rad(Z_DEGREES)).lookVector
4 Likes

Terrific! Thanks guys!

Hey guys, I’m still struggling a bit trying to get my rays to cast at a consistent angle. It just doesn’t work at all when I use your method, Vital. I don’t know why, it looks something like this:

ray = Ray.new(pin.Position,CFrame.Angles(math.rad(25),0,0).lookVector * 5)

It just doesn’t do anything with the angles at all :confused:

@IdiomicLanguage,

I just couldn’t figure out how to apply your code in doing this, I’m not very smart when it comes to this stuff… :frowning:

Looking at your code, your construction of the ray seems to be correct. It will create a ray that starts at your pin position and point at an angle 25 degrees up, and with a distance of 5 studs.

Maybe you expect it to act differently than you think.
The direction is always the same. If you want the direction to change with the pin, then you would want to put the angle in it’s local space before getting the world lookvector.

You could also try debugging it and figuring out exactly where your code deviates from what you’d like it to do. Possibly create an anchored part at the raycast endpoint to make sure it is going where you expect.

1 Like

If you give me a bigger picture of what you are trying to do, I wouldn’t mind working with you to figure it out.

1 Like

I agree with VitalWinter. I’d like to help you, but to help you apply it, I’ll need to know what you are up to. To get more familiar, here is a reply I recently posted on a similar question:

Thanks for the replies, guys.

So what I’m trying to do, in a nutshell, is cast a ray at a position, then for it’s directional value I want to set in degrees. Like, from the origin position, cast the ray at a 45 degree angle to the right, or left, or whatever it might be. I just can’t seem to figure out how to angle this ray at all, as nothing I do seems to get it to angle from the origin position whatsoever, any further help appreciated!

I’ve figured it out! :smile:

Thanks guys for the help and stuff, I do appreciate it. :slight_smile:

Could you post some example code or something if possible as it may be useful to others in the future.:upside_down_face:

1 Like

Sure thing.

So by using an algorithm similar to this, I’m able to successfully cast a ray from an origin position, with any desired angle I want. (in degrees, how simple is that?!)

local ray = Ray.new(pin.Pin.Position,CFrame.Angles(0,math.rad(90),0).lookVector * length)

6 Likes

for a directional launcher it should probably be

Ray.new(pin.Pin.Position,(pin.Pin.CFrame*CFrame.Angles(0,math.rad(90),0)).lookVector * length)
1 Like