Hello, I am very confused with something, I have a look on the dev forum and it isnt what I am looking for exactly. All I can find is spread with guns for example not how I need it to work. For example raycasting is one beem shooting from a beam but I would like to try and make it like such:
local test = workspace.test
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {workspace.Folder}
local function fireRay(angle)
local direction = (test.CFrame * CFrame.Angles(0,math.rad(angle),0)).LookVector
local result = workspace:Raycast(test.Position, direction * 100, params)
if result then
local part = Instance.new("Part")
part.Size = Vector3.new(.5,.5,.5)
part.Color = Color3.fromRGB(255,0,0)
part.Anchored = true
part.CanCollide = false
part.Position = result.Position
part.Parent = workspace.Folder
end
end
for i = -45, 45 do
fireRay(i)
end
Instead of shooting 15 raycasts (single lines) out at multiple angles and possibly missing items you can use less Shapecasts and have a better chance at hitting something (within reason of course, depending on how close you plan on being to the items(s) the shapecasts hit).
What would you recommend sphere, block and the other one can’t remember the name for this case or does it depend and how does shape cast work on a moving object will it miss things or depends?
Please tell us exactly what you are trying to do. We can probably help you better.
Precise details like the following really would help:
How far do you want the casts to go? 2-3 studs or 500 studs?
What’s the angle of spread you want to achieve?
How big are the Parts you are trying to detect with the casts?
Are the Parts you are trying to detect moving toward the casting Part, or is the casting Part moving?
How far do you want the casts to go? 2-3 studs or 500 studs?
100 studs
What’s the angle of spread you want to achieve?
90 degrees
How big are the Parts you are trying to detect with the casts?
It depends really but mainly a player.
Are the Parts you are trying to detect moving toward the casting Part, or is the casting Part moving?
Casting part moving.
It all depends then.
For a 90 degree spread with a player that’s 4 studs wide (yeah, if they are sideways they are approximately 1 stud wide, but chances of that are slim). You’d need a ray every 2.3 degrees, so you’d need approximately 40 rays.
If you did 90 degrees with a 1 stud square shapecast you’d be looking at about 3.1 degrees and 29 shapecasts.
Raycasts are just that, a single ray which is just a line through space with no thickness. Raycasts are useful for checking for line-of-sight between two points, or to get the exact intersection point or distance to a surface.
If you want to detect objects in a particular direction, you could use a lot of raycasts, but it might not be the best option for the first phase of your detection scheme. It depends a bit on what you’re trying to detect. If it’s just player avatars, or some similarly smallish set of objects, you might do something like first check the distance from the source (whatever that square is in your drawing) to each object, and see which ones are < 100 studs away. Then, check the angle between the direction from source to object, and the direction the source thing is pointing. Cull everything outside of the desired spread angle. Then, once you have this set of objects down to just the ones that are inside the vision cone/wedge, do a raycast to each one if you need to check for line of sight (unobstructed view).
This sort of arrangement might be more suitable than lots of raycasts. Supposing you only need a flat fan of rays, with a radius of 100 studs and an included angle of 90 degrees, that’s a 157-stud arc at the far end. To be sure not to miss the worst case–a sideways player 100 studs away with 1-stud HRP thickness–you’d need a minimum of 158 raycasts. If there is height to that search volume, it only gets worse.
This makes so much sense now omg, thank you so much, I understand how to do this after this. Do you know how well to optimise this or do raycasts not cause much issues for users on mobile or something like that. The amout of workspace:raycasts are going to be loads . Anways Thank you for your help everyone!
This may not fit your exact use case, in one game I made, there were baddie NPCs that could “see” with a particular horizontal field of view, let’s say within +/- 45 degrees of the direction they were facing. The code to see if the player was within the NPC’s horizontal vield of view looked something like this:
local vectorToPlayerXZ = (playerHRP.Position - npcHRP.Position) * Vector3.new(1,0,1)
local dirToPlayerXZ = vectorToPlayerXZ.Unit
local npcFacingDirXZ = npcHeadPart.CFrame.LookVector * Vector3.new(1,0,1)
local dotProduct = npcFacingDirXZ:Dot(dirToPlayerXZ)
local inFieldOfView = dotProduct >= 0.7071
The hard-coded 0.7071 comes from math.cos(math.rad(45)) for +/- 45 deg FoV
This is because the arc cosine of the dot product of two unit vectors is the angle between them (in Radians in the case of Lua math.acos), but there’s no need to call math.acos at all because you can just pre-compute the cosine of 45 degrees and compare it directly to the dot product.
The multiplies with (1,0,1) are just removing the world Y component from all of the positions, so that the math is just being done in the horizontal (XZ) plane of world space.
Some times this is all you need, like if zombies are just looking for players on flat, level ground, and you don’t care about if they are at a different height, or if it matters if they are looking up or down (head tilt I mean). Other times, you might want a vertical field of view in the space of the NPC’s head too, which is just more of this same kind of math, but instead of being done in world space, it’s simplest to just transform all the player positions into the coordinate space of the NPC’s head first (PointToObjectSpace).