How to fire a ray in a random direction?

I’m trying to fire a ray in a random direction. However, it seems to be going in the same direction each time. I can confirm that randomVector is unique every single time, so I’m not sure what’s happening.

local random = Random.new()
local randomVector = Vector3.new(random:NextNumber(), random:NextNumber(), random:NextNumber()).Unit
local distance = 1000
local Ricochet = ReplicatedStorage.Clones.Ricochet:Clone()
Ricochet.Size = Vector3.new(Ricochet.Size.X, Ricochet.Size.Y, distance)
Ricochet.CFrame = CFrame.new(coinShot.Position, randomVector) * CFrame.new(0,0,-distance/2)
Ricochet.Parent = workspace	
Debris:AddItem(Ricochet, 0.1)
2 Likes

There’s no raycasting code??? chars chars chars

1 Like

It’s not raycast, they’re talking about the throwing target.

it just needs a bigger number

local random = Random.new()

local randomVector = Vector3.new(
	math.random(-math.pi * 90, math.pi * 90),
	math.random(-math.pi * 90, math.pi * 90),
	math.random(-math.pi * 90, math.pi * 90)
)

local distance = 1000
local Ricochet = ReplicatedStorage.Clones.Ricochet:Clone()
Ricochet.Size = Vector3.new(Ricochet.Size.X, Ricochet.Size.Y, distance)
Ricochet.CFrame = CFrame.new(coinShot.Position, randomVector) * CFrame.new(0,0,-distance/2)
Ricochet.Parent = workspace	
Debris:AddItem(Ricochet, 0.1)
2 Likes

Thanks this worked, could you explain a bit more as to why my code didn’t work though? Aren’t LookVector values between the range of 0 and 1? I thought it would’ve worked if I just generated a random decimal between 0 and 1.

maybe look vectors are just in degrees instead of radians or unit

CFrame.new(coinShot.Position, randomVector)

CFrame.new(Vector3, Vector3) creates a CFrame whose position is point A and whose rotation is pointing from point A to point B. Point B is not a direction (as your script has it), it is a position.

Your code is pointing that CFrame from Shot’s position toward something really close to (0, 0, 0). The direction isn’t always the same, it’s simply directed toward the origin, go anywhere else and the direction of the coin will change.

The accepted answer “works” because it’s pointing from the coin’s position to a kinda random position. If you’re near the origin, the direction really will be kinda random. If you go 1000 studs away from the origin, it will start tossing the coin toward the “same direction every time” again because it’s still pointing toward a position, not in the direction you created.

Do this instead:

Ricochet.CFrame = CFrame.new(coinShot.Position, coinShot.Position + randomVector) * CFrame.new(0,0,-distance/2)

Now it’s located at coinShot.Position and pointing in the same direction as the random vector, regardless of where the coin is.
Point B is now an absolute position, not a relative direction.
Even better:

local pos = coinShot.Position - randomVector * (distance / 2)
Ricochet.CFrame = CFrame.new(pos, pos + randomVector)

(I can’t be certain whether pos should have a -randomVector*distance or a +randomVector*distance)

While you’re at it, use a uniformly random direction (source):

local a = 2*math.pi*math.random()
local x = 2*math.random() - 1
local r = math.sqrt(1 - x*x)
local y, z = r*math.cos(a), r*math.sin(a)
local randomVector = Vector3.new(x, y, z)

A LookVector is a unit vector, which is any vector whose length (.Magnitude) is 1. It has little to do with radians or degrees, it’s just three otherwise uncorrelated numbers whose squares add up to 1.

For example, if you get the .Unit of the position of some random thing in workspace, then you get the direction pointing from (0, 0, 0) toward that position.

You should ask your maths teacher to explain this stuff to you.

2 Likes

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