Advertisement
Cat_in_the_hat

Combat detection angle

Feb 17th, 2025 (edited)
126
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.91 KB | Gaming | 0 0
  1. local woolBlocks = BlockService.getAllBlocks({ ItemType.WOOL_RED })
  2. local radius = 25
  3. local angleOffset = math.rad(80)
  4. local partSize = Vector3.new(0.31, 0.2, 0.31)
  5. local rotationOffset = CFrame.Angles(0, math.rad(0.80), 0)
  6. local step = partSize.X
  7.  
  8. if #woolBlocks > 0 then for _, woolBlock in pairs(woolBlocks) do local center = woolBlock.position + Vector3.new(0, 1.5, 0)
  9.  
  10. local rightLineParts = {}  
  11.     local leftLineParts = {}  
  12.     local circleParts = {}  
  13.     local topParts = {}  
  14.  
  15.     local function createPart(position, size, lookAtTarget, partList)    
  16.         local direction = (lookAtTarget - position).Unit  
  17.         local part = PartService.createPart(ItemType.CLAY_WHITE, position)    
  18.         part:setSize(size)    
  19.         part:setCFrame(CFrame.lookAt(position, position + direction) * rotationOffset)    
  20.         table.insert(partList, part)    
  21.         return part    
  22.     end    
  23.  
  24.     for i = 1, math.floor(radius * 3) + 2 do    
  25.         local angleStep = (i / (radius * 3)) * math.pi * 2    
  26.         local pos = center + Vector3.new(math.cos(angleStep) * (radius + 0.3), 0, math.sin(angleStep) * (radius + 0.3))    
  27.         createPart(pos, Vector3.new(1, 0.2, 0.3), center, circleParts)    
  28.     end    
  29.  
  30.     for distance = step, radius + step, step do    
  31.         for _, a in ipairs({ angleOffset, -angleOffset }) do  
  32.             local pos = center + Vector3.new(math.cos(a) * distance, 0, math.sin(a) * distance)  
  33.             local partList = (a == angleOffset) and rightLineParts or leftLineParts  
  34.             createPart(pos, partSize, center, partList)    
  35.         end  
  36.     end    
  37.  
  38.     local angleFactor = math.abs(angleOffset) / math.rad(50)  
  39.     local capPartsCount = math.ceil((radius * 2) / partSize.X * angleFactor)    
  40.  
  41.     for i = 0, capPartsCount do    
  42.         local t = i / capPartsCount    
  43.         local capAngle = (angleOffset * (1 - t)) + (-angleOffset * t)    
  44.         local capPos = center + Vector3.new(math.cos(capAngle) * (radius + 0.3), 0, math.sin(capAngle) * (radius + 0.3))    
  45.         createPart(capPos, partSize, center, topParts)    
  46.     end    
  47.  
  48.     local newCapPartsCount = #topParts
  49.     for i, part in ipairs(topParts) do    
  50.         local t = i / (newCapPartsCount + 1)  
  51.         local capAngle = (angleOffset * (1 - t)) + (-angleOffset * t)    
  52.         local capPos = center + Vector3.new(math.cos(capAngle) * (radius + 0.3), 0, math.sin(capAngle) * (radius + 0.3))    
  53.         part:setPosition(capPos)    
  54.         part:setCFrame(CFrame.lookAt(capPos, woolBlock.position + Vector3.new(0, 1.5, 0)) * rotationOffset)  
  55.     end
  56.  
  57.     local extraOffset = math.rad(5)  
  58.     local extraLeftCapPos = center + Vector3.new(math.cos(-angleOffset + extraOffset) * (radius + 0.3), 0, math.sin(-angleOffset + extraOffset) * (radius + 0.3))    
  59.     local extraRightCapPos = center + Vector3.new(math.cos(angleOffset - extraOffset) * (radius + 0.3), 0, math.sin(angleOffset - extraOffset) * (radius + 0.3))    
  60.  
  61.     createPart(extraLeftCapPos, partSize, center, topParts)    
  62.     createPart(extraRightCapPos, partSize, center, topParts)    
  63.  
  64.     task.spawn(function()  
  65.         while true do  
  66.             local nearestPlayer = nil    
  67.             local nearestDistance = radius    
  68.             local entities = EntityService.getNearbyEntities(center, radius)    
  69.  
  70.             for _, entity in ipairs(entities) do    
  71.                 local player = entity:getPlayer()    
  72.                 if player then    
  73.                     local playerPos = entity:getPosition()    
  74.                     local dist = (playerPos - center).Magnitude    
  75.                     if dist < nearestDistance then    
  76.                         nearestDistance = dist    
  77.                         nearestPlayer = player    
  78.                     end    
  79.                 end    
  80.             end    
  81.  
  82.             if nearestPlayer then    
  83.                 local playerPos = nearestPlayer:getEntity():getPosition()    
  84.                 local direction = (playerPos - center)    
  85.  
  86.                 if direction.Magnitude > 0 then    
  87.                     direction = Vector3.new(direction.X, 0, direction.Z).Unit    
  88.                 else    
  89.                     direction = Vector3.new(0, 0, 1)    
  90.                 end    
  91.  
  92.                 local playerAngle = math.atan2(direction.Z, direction.X)    
  93.                 local leftAngle = playerAngle + angleOffset    
  94.                 local rightAngle = playerAngle - angleOffset    
  95.  
  96.                 for i, part in ipairs(leftLineParts) do    
  97.                     local distance = step * i    
  98.                     local newPos = center + Vector3.new(math.cos(leftAngle) * distance, 0, math.sin(leftAngle) * distance)    
  99.                     part:setPosition(newPos)    
  100.                     part:setCFrame(CFrame.lookAt(newPos, woolBlock.position + Vector3.new(0, 1.5, 0)) * rotationOffset)  
  101.                 end    
  102.  
  103.                 for i, part in ipairs(rightLineParts) do    
  104.                     local distance = step * i    
  105.                     local newPos = center + Vector3.new(math.cos(rightAngle) * distance, 0, math.sin(rightAngle) * distance)    
  106.                     part:setPosition(newPos)    
  107.                     part:setCFrame(CFrame.lookAt(newPos, woolBlock.position + Vector3.new(0, 1.5, 0)) * rotationOffset)  
  108.                 end    
  109.  
  110.                 for i, part in ipairs(topParts) do    
  111.                     local t = i / (newCapPartsCount + 1)    
  112.                     local capAngle = (leftAngle * (1 - t)) + (rightAngle * t)    
  113.                     local capPos = center + Vector3.new(math.cos(capAngle) * (radius + 0.3), 0, math.sin(capAngle) * (radius + 0.3))    
  114.                     part:setPosition(capPos)    
  115.                     part:setCFrame(CFrame.lookAt(capPos, woolBlock.position + Vector3.new(0, 1.5, 0)) * rotationOffset)  
  116.                 end    
  117.             end    
  118.  
  119.             task.wait(0)    
  120.         end    
  121.     end)    
  122. end
  123.  
  124. end
  125.  
  126.  
Advertisement
Comments
  • Cat_in_the_hat
    4 days (edited)
    # C++ 0.25 KB | 0 0
    1. Showcase : https://discord.com/channels/1132704840313749615/1341227620926492752/1341227620926492752
    2. Note : place down a wool_red block on ground for Script  work this a script for coders can use so it only has radius and find nearby entity's function
Add Comment
Please, Sign In to add comment
Advertisement