In this image, I tried to represent what I want to achieve. See, the red point, which represents the player’s camera position and the cone tip, is where the raycasts would begin. That part is simple. Now, achieving a random direction on a circle (the cone base) is what I don’t know how to do. The blue lines are a basic representations of 3 raycasts, and the wall is a wall. How can I do such thing?
You could get a random ray on the surface of a cone with a function like the one below. coneTip
is the CFrame of the point of the cone facing the center of the base of the cone (this is your Camera.CFrame
). coneAngle
is expected to be in degress and is the angle from the altitude of the cone.
function getRandomRayOnCone(coneTip: CFrame, coneAngle: number): Ray
local zRot: CFrame = coneTip * CFrame.fromAxisAngle(Vector3.zAxis, 2*math.pi*math.random())
local inclined: CFrame = zRot * CFrame.fromAxisAngle(Vector3.xAxis, math.rad(coneAngle))
return Ray.new(inclined.Position, inclined.LookVector)
end
This will return a Ray with length 1 on the surface of the cone. Keep in mind if you use this for a raycast you should multiply the Ray.Direction
by the distance you want the ray to travel.
It’d help a lot if you tell us why you need random raycasts in the outer diameter of a cone shape.
For example do you mean you want a series of raycasts in the cone to detect all it’s outer edge hits?
Or is this something like a gun shooting with a bullet spread pattern and you want to place the random spread of the bullets just in this area.
You can use shapecasts (Introducing generalized shapecasts) and make a part the shape and size of your cone. It’ll shoot 1 shapecast and pick up anything hit in that shape Part.
Ah, any way I could include the inside?
Exactly this, I want to include the inside of the cone aswell.
I avoid using that. I specifically need to fire multiple lines in a cone shape instead of directly firing the cone. Basically, a LIDAR gun.
Yes, there are ways to make it a Ray within a Cone. This is one approach:
function getRandomRayInCone(coneTip: CFrame, coneAngle: number): Ray
local zRot: CFrame = coneTip * CFrame.fromAxisAngle(Vector3.zAxis, 2*math.pi*math.random())
local inclined: CFrame = zRot * CFrame.fromAxisAngle(Vector3.xAxis, math.random()*math.rad(coneAngle))
return Ray.new(inclined.Position, inclined.LookVector)
end
However, this will favor Rays closer to the altitude of the Cone. If you want the Rays to be evenly distributed you would need to take a different approach.
So make a MeshPart as a hollow cone to use as your shape.
How would that help exactly? Here’s a visual representation of what I’m trying to make.
So it will generate rays closer to the center of the base (circle) or closer to the edges…?
The cone is purely a visual shape, it limits the directions of the raycasts.
Closer to the center. Run this code if you want to see it for yourself:
--In a Script in ServerScriptService
function getRandomRayInCone(coneTip: CFrame, coneAngle: number): Ray
local zRot: CFrame = coneTip * CFrame.fromAxisAngle(Vector3.zAxis, 2*math.pi*math.random())
local inclined: CFrame = zRot * CFrame.fromAxisAngle(Vector3.xAxis, math.random()*math.rad(coneAngle))
return Ray.new(inclined.Position, inclined.LookVector)
end
local folder = Instance.new("Folder", workspace)
folder.Name = "ConeRays"
for i = 1, 1000 do
local ray = getRandomRayInCone(CFrame.new(0,10,0), 30)
local part = Instance.new("Part", folder)
part.Color = Color3.new(1,0,0)
part.Size = Vector3.new(0.1,0.1,0.1)
part.Anchored = true
part.CFrame = CFrame.new(ray.Origin + 8*ray.Direction)
task.wait()
end
Mm, I will test that in a few hours. I’ll also use workspace:Raycast instead of a Ray since, personally, I don’t really trust those. Would work the exact same programatically since both the origin and directions are there.
Does what I want to achieve have a more specific name? All I tried searching for on the web resulted in different approaches
I mean a Ray is just a datatype that intuitively makes sense to use for a raycast. With the code I’ve provided:
local ray: Ray = getRandomRayInCone(CFrame.new(), 30)
local rayRes: RaycastResult = workspace:Raycast(ray.Origin, 8*ray.Direction, RAYCAST_PARAMS)
If you want the ray to be more uniformly dispersed within the cone then what you might be looking for is a method to get a “uniformly distributed ray inside a cone”. That search might yield something closer to what you’re looking for.
Alright, I’ll see what I can find and also try the code you gave and see what results I get, then I’ll get back to you. Thanks!
Ah, so not really a cone shape, a fan shape that scans up to down in a round pattern, with visual effects indicating different scan lines.
i dont know if this is relevant, but this is a really neat way to use axis angles that i would never have thought of. so elegant!
I was just rethinking this.
Since you want a cone shaped ‘scan’ that works its way down, couldn’t you Tween the width of the scan using EasingStyle Circular In for the first half, then Out for the second half (or Out then In, whichever gives you the round end shape.
Hmm
What if I change this to “randomly directed”?
I am still not sure you understand the type of raycast I’m trying to do, so let me rephrase:
- Have a cone, no matter the height or angle, just a cone
- The tip of the cone (sharp point) would be the origin for all raycasts
- Raycasts should hit anywhere, randomly, on the base (circle). Anyyywhere on the base.
- Their direction is limited by the cone, so that would mean no end point can generate outsidw if the circle, hence the base
- All of that, while only knowing the origin and the origin’s LookVector (essentially, the tip of the cone is the Camera CFrame and the orientation of the cone is the direction of the camera)
There is no conecast, just raycasts inside a cone shape
Ah, k.
The video you linked before showed a ‘scanning’ red visual effect, top to bottom, that I thought you were trying to create as well…
I’d try to recreate that aswell, wasn’t sure what you were talking about. I tried for starters to make the first effect
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.