function earlyReflections(numBounces)
local rayOrigin = head.Position
local speakerPosition = speaker.Position -- Get the position of the speaker part
for i = 1, numBounces do
local theta = math.rad(math.random(0, 360)) -- Azimuthal angle
local phi = math.rad(math.random(0, 180)) -- Polar angle (0 to 180 degrees)
local randomDirection = Vector3.new(
math.sin(phi) * math.cos(theta), -- x
math.sin(phi) * math.sin(theta), -- y
math.cos(phi) -- z
).Unit * 100
local currentOrigin = rayOrigin
local currentDirection = randomDirection
local SURFACE_OFFSET = 0.05
local rayResult = raycast(currentOrigin, currentDirection, {character})
if rayResult then
createRayPart(currentOrigin, rayResult.Position)
local directionToSpeaker = (speakerPosition - rayResult.Position).Unit -- This is now a unit vector
local distanceToSpeaker = (speakerPosition - rayResult.Position).Magnitude
currentDirection = directionToSpeaker * distanceToSpeaker
createRayPart(rayResult.Position, speakerPosition)
break
else
createRayPart(currentOrigin, currentOrigin + currentDirection.Unit * 100)
break
end
end
end
so I have about 200 rays at once, but from the picture you can clearly see that there doesn’t seem to be 200, but the reality is that most are superimposing on the same location. My question is why, I am using math.random yes?
function raycast(origin, direction, excludeInstances)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = excludeInstances
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
if raycastResult then
print("Raycast hit: " .. raycastResult.Instance.Name)
else
print("Raycast did not hit anything.")
end
return raycastResult
end
I’ll remove createRayPart(currentOrigin... and let you know what happens.