Hi, soo i’m working on a nuke system that will shoot rocket to sky, then make it go boom, recently i realized that this area will be polygon, like 4k/4k studs, the problem is that i don’t have any clues on how to detect if player is inside it or not
I found that 4 rays shooted in 4 directions from player can help with this, as if every hits walls or parts that represents them, i can detect if player is inside area or not, is there any alternative to that???
well you can just get the least and biggest Z position value from your area, same with X and then some Y value. then check if the player’s root part position is in it.
say you have a square that reaches from 0 to 15 & on x -5 to 20 then you’d check if the player’s x and z are in between those values:
if root.Position.X > 20 or root.Position.X < -5 then return end
if root.Position.Z > 15 or root.Position.Z < 0 then return end
--player is in zone, do something...
in this code, player 's root part is supposed to be root. and the numbers are the bounds of your zone.
If your zone’s center is the nuke, you can get the bounds of the zone and then check which players are in that zone:
local nuke = --your nuke
local zoneSize = 4000--size of the nuke's area.
local function getPlayersInNuke(): {Player}
--assuming nuke has a position property, otherwise, replace with the actual position.
local minbound = nuke.Position - Vector3.new(zoneSize/2, 0, zoneSize/2)
local maxbound = nuke.Position + Vector3.new(zoneSize/2, 0, zoneSize/2)
local players = {}
for _, plr in ipairs(game.Players:GetPlayers()) do
if not plr.Character then continue end
local rootpos = plr.Character.PrimaryPart.Position
if rootpos.X < minbound.X or rootpos.X > maxbound.X then continue end
if rootpos.Z < minbound.Z or rootpos.Z > maxbound.Z then continue end
table.insert(players, plr)
end
return players
end
and thhen you can do something like:
local targets = getPlayersInNuke()
for _, v in ipairs(targets) do v.Character.Humanoid.Health = 0 end
this is the easiest way I can think of, to do this, but there are other more complex ways to check if an object is inside an area.
shi, my reading comprehension is down today I guess?! How could I have missed that.
so uhh, that requires a more complex algorithm, I was explaining it but I just found the wiki for it so I’ll link it here: Point in polygon - Wikipedia
hope this helps?
(tldr: cast a ray from the objects position and if the amount of times the ray intersects with the edges of the polygon is even the object is inside of the polygon, otherwise it isn’t)
oh, well, I don’t think so, I mean obviously there are but I think this one is the easiest way. So you have technically solved your own problem already, so yeah…
yea xd, i think that rays are the most efficient one, using math to search a lot of lines is worse than having 10 invisible parts and 4 rays every 1-2 seconds, thx for help
I’m not sure this is exactly what you are going for, but isnt the Touched function an much easier way to do so? I made a script so you get a better undestanding:
local polygon = game.workspace.yourpolygon -- get your polygon once the event is happening
polygon.Touched:Connect(function(plr) -- something touched the part!
local humanoid = plr.Parent:FindFirstChildOfClass("Humanoid") -- let's create a variable to see if a player touched the part
if humanoid then-- if humanoid was found then a player touched!
print(plr.Parent) -- prints the name of what touched the part, the player!
-- you can use just print(plr) if you want to know what specific part touched (ex: torso, left hand etc.)
-- you can do the rest of your script here!
end
end)
I guess you could implement this script into the polygon IF you’re gonna be cloning one into workspace, if not you can just make and invisible part and make it appear when the nuke goes boom.