How to fire a ray with a random yaw direction?

Good morning.

I want to shoot rays at random points on the horizon. This way the NPC will pick an empty space.
But even with this minimal script I had problems. The rays never hit anything.

local function getRandomPointInCircle(centerPosition)
	local rayOrigin = centerPosition
	local RANDOM_DEGREES = math.random(0, 360)
	local rayDirection = CFrame.Angles(math.rad(90), math.rad(RANDOM_DEGREES), math.rad(0)).lookVector
	
	local ray = Ray.new(rayOrigin, rayDirection)
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
	
	print(raycastResult)
end

for i = 1, 100 do
	getRandomPointInCircle(Vector3.new(0, 5, 0))
	task.wait(0.1)
end

I tried to visualize the rays with parts, but they all have magnitude 1. I’ve also tried to shoot rays in every direction, but they still don’t hit anything.

Any help would be appreciated!

local endPosition

if not raycastResult then
  endPosition = rayOrigin + rayDirection
  print (endPosition)
else
  print (raycastResult)
end

should work fine. Or I’m an idiot and this didn’t solve anything.

Hi.
You’re not an idiot and…
Your snippet outputs coordinates close to the origin. I guess it’s because it adds radians to that position.
Thanks for your attention!

I tried to calculate a random direction by subtracting two positions, to no avail. Rays are driving me nuts!

local function getRandomPointInCircle(centerPosition)
	local rayOrigin = centerPosition
	local rayPosition = Vector3.new(centerPosition.X + math.random(-10, 10), centerPosition.Y, centerPosition.Z + math.random(-10, 10))
	local rayDirection = rayPosition - rayOrigin
	
	local ray = Ray.new(rayOrigin, rayDirection)
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
	
	print(raycastResult)
end

Hello, @TheBrainyAnimator

This is one way to create a random direction:

function castRandomDirection(orgin: Vector3, length: number): Ray
	-- Converting between radians and degrees on a large scale is not efficient
	local rotation: number = math.random(0, math.pi * 2)

	local direction: Vector3 = Vector3.new(math.cos(rotation), 0, math.sin(rotation)) * length
	
	return workspace:Raycast(orgin, direction)
end

-- Additional code (ignore)
for i = 1, 100 do
	local randomRay: Ray = castRandomDirection(Vector3.new(0, 0, 0), 100)
	if randomRay then
		print(randomRay)
	end
	
	task.wait(1)
end

You must multiply the desired ray’s length by a normalized vector/direction when using raycasting.
The length in this instance is 100.


Quote from official documentation

When casting a ray, the distance between the origin and directional Vector3 is the functional length (magnitude) of the ray. The maximum length is 15,000 studs.

for example if we have a direction like this:

local rayDirection = Vector3.new(0, 100, 0)
-- print(Vector3.new(1, 1, 0).Unit * 100)
local secondRayDirection = Vector3.new(70.71068572998047, 70.71068572998047)

Both will only travel 100 studs in said direction.

I suspect that your issue is either:

  1. The ray direction is not calculated as expected
  2. The ray was too short and couldn’t hit anything
  3. The ray was hitting the inside of a part that wasn’t excluded

However, since I’m very late, you’ve probably already fixed it yourself.
Not that this is necessarily the problem, of course.
It is possible that my information is inaccurate and I am entirely mistaken.
I would be happy to help in any circumstance!

Official raycasting guide

Example raycasts with 100 stud length

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.