Advertisement
Cat_in_the_hat

Combat detection angle (update(2))

Feb 17th, 2025 (edited)
241
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.93 KB | Gaming | 0 0
  1. local woolBlocks = BlockService.getAllBlocks({ ItemType.WOOL_RED })  
  2. local damage = 1  
  3. local hitspeed = 0.1  
  4. local radius = 25  
  5. local angleOffset = math.rad(80)  
  6. local minAttackHeight  = -9
  7. local maxAttackHeight = 3  
  8.  
  9. -- don't touch
  10. local partSize = Vector3.new(0.31, 0.2, 0.31)  
  11. local rotationOffset = CFrame.Angles(0, math.rad(0.80), 0)  
  12. local step = partSize.X  
  13. local rightLineParts = {}  
  14. local leftLineParts = {}  
  15. local circleParts = {}  
  16. local topParts = {}  
  17.  
  18. if #woolBlocks > 0 then  
  19.     for _, woolBlock in pairs(woolBlocks) do  
  20.         local center = woolBlock.position + Vector3.new(0, 1.5, 0)  
  21.  
  22.         local function createPart(position, size, lookAtTarget, partList)  
  23.             local direction = (lookAtTarget - position).Unit  
  24.             local part = PartService.createPart(ItemType.CLAY_WHITE, position)  
  25.             part:setSize(size)  
  26.             part:setCollidable(false)  
  27.             part:setCFrame(CFrame.lookAt(position, position + direction) * rotationOffset)  
  28.             table.insert(partList, part)  
  29.             return part  
  30.         end  
  31.  
  32.         for i = 1, math.floor(radius * 3) + 2 do  
  33.             local angleStep = (i / (radius * 3)) * math.pi * 2  
  34.             local pos = center + Vector3.new(math.cos(angleStep) * (radius + 0.3), 0, math.sin(angleStep) * (radius + 0.3))  
  35.             createPart(pos, Vector3.new(1, 0.2, 0.3), center, circleParts)  
  36.         end  
  37.  
  38.         for distance = step, radius + step, step do  
  39.             for _, a in ipairs({ angleOffset, -angleOffset }) do  
  40.                 local pos = center + Vector3.new(math.cos(a) * distance, 0, math.sin(a) * distance)  
  41.                 local partList = (a == angleOffset) and rightLineParts or leftLineParts  
  42.                 createPart(pos, partSize, center, partList)  
  43.             end  
  44.         end  
  45.  
  46.         local angleFactor = math.abs(angleOffset) / math.rad(50)  
  47.         local capPartsCount = math.ceil((radius * 2) / partSize.X * angleFactor)  
  48.         for i = 0, capPartsCount do  
  49.             local t = i / capPartsCount  
  50.             local capAngle = (angleOffset * (1 - t)) + (-angleOffset * t)  
  51.             local capPos = center + Vector3.new(math.cos(capAngle) * (radius + 0.3), 0, math.sin(capAngle) * (radius + 0.3))  
  52.             createPart(capPos, partSize, center, topParts)  
  53.         end  
  54.  
  55.         local newCapPartsCount = #topParts  
  56.         for i, part in ipairs(topParts) do  
  57.             local t = i / (newCapPartsCount + 1)  
  58.             local capAngle = (angleOffset * (1 - t)) + (-angleOffset * t)  
  59.             local capPos = center + Vector3.new(math.cos(capAngle) * (radius + 0.3), 0, math.sin(capAngle) * (radius + 0.3))  
  60.             part:setPosition(capPos)  
  61.             part:setCFrame(CFrame.lookAt(capPos, center) * rotationOffset)  
  62.         end  
  63.  
  64.         local extraOffset = math.rad(5)  
  65.         local extraLeftCapPos = center + Vector3.new(math.cos(-angleOffset + extraOffset) * (radius + 0.3), 0, math.sin(-angleOffset + extraOffset) * (radius + 0.3))  
  66.         local extraRightCapPos = center + Vector3.new(math.cos(angleOffset - extraOffset) * (radius + 0.3), 0, math.sin(angleOffset - extraOffset) * (radius + 0.3))  
  67.         createPart(extraLeftCapPos, partSize, center, topParts)  
  68.         createPart(extraRightCapPos, partSize, center, topParts)  
  69.  
  70.         task.spawn(function()  
  71.             while true do  
  72.                 local nearestPlayer = nil  
  73.                 local nearestDistance = radius  
  74.                 local entities = EntityService.getNearbyEntities(center, radius)  
  75.  
  76.                 for _, entity in ipairs(entities) do  
  77.                     local player = entity:getPlayer()  
  78.                     if player then  
  79.                         local playerPos = entity:getPosition()  
  80.                         local dist = (playerPos - center).Magnitude  
  81.  
  82.                         local relativeY = playerPos.Y - center.Y  
  83.                         if relativeY > maxAttackHeight or relativeY < minAttackHeight then  
  84.                             continue  
  85.                         end  
  86.  
  87.                         if dist < nearestDistance then  
  88.                             nearestDistance = dist  
  89.                             nearestPlayer = player  
  90.                         end  
  91.                     end  
  92.                 end  
  93.  
  94.                 if nearestPlayer then  
  95.                     local playerPos = nearestPlayer:getEntity():getPosition()  
  96.                     local direction = (playerPos - center)  
  97.                     if direction.Magnitude > 0 then  
  98.                         direction = Vector3.new(direction.X, 0, direction.Z).Unit  
  99.                     else  
  100.                         direction = Vector3.new(0, 0, 1)  
  101.                     end  
  102.  
  103.                     local playerAngle = math.atan2(direction.Z, direction.X)  
  104.                     local leftAngle = playerAngle + angleOffset  
  105.                     local rightAngle = playerAngle - angleOffset  
  106.  
  107.                     for _, entity in ipairs(entities) do  
  108.                         local entityPos = entity:getPosition()  
  109.                         local entityAngle = math.atan2(entityPos.Z - center.Z, entityPos.X - center.X)  
  110.                         local entityDist = (entityPos - center).Magnitude  
  111.  
  112.                         if rightAngle <= entityAngle and entityAngle <= leftAngle and entityDist <= radius then  
  113.                             CombatService.damage(entity, damage)  
  114.                         end  
  115.                     end  
  116.  
  117.                     for i, part in ipairs(leftLineParts) do  
  118.                         local distance = step * i  
  119.                         local newPos = center + Vector3.new(math.cos(leftAngle) * distance, 0, math.sin(leftAngle) * distance)  
  120.                         part:setPosition(newPos)  
  121.                         part:setCFrame(CFrame.lookAt(newPos, center) * rotationOffset)  
  122.                     end  
  123.  
  124.                     for i, part in ipairs(rightLineParts) do  
  125.                         local distance = step * i  
  126.                         local newPos = center + Vector3.new(math.cos(rightAngle) * distance, 0, math.sin(rightAngle) * distance)  
  127.                         part:setPosition(newPos)  
  128.                         part:setCFrame(CFrame.lookAt(newPos, center) * rotationOffset)  
  129.                     end  
  130.  
  131.                     for i, part in ipairs(topParts) do  
  132.                         local t = i / (newCapPartsCount + 1)  
  133.                         local capAngle = (leftAngle * (1 - t)) + (rightAngle * t)  
  134.                         local capPos = center + Vector3.new(math.cos(capAngle) * (radius + 0.3), 0, math.sin(capAngle) * (radius + 0.3))  
  135.                         part:setPosition(capPos)  
  136.                         part:setCFrame(CFrame.lookAt(capPos, center) * rotationOffset)  
  137.                     end  
  138.                 end  
  139.  
  140.                 task.wait(hitspeed)  
  141.             end  
  142.         end)  
  143.     end  
  144. end
Advertisement
Comments
  • Cat_in_the_hat
    124 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
  • Cat_in_the_hat
    112 days
    # Perl 0.30 KB | 0 0
    1. `note: update 2 I have added damge since you guys been asking me to add `
    2. Showcase: https://media.discordapp.net/attachments/1341227620926492752/1344884554481012847/Screen_Recording_20250227_211701_Roblox.mp4?ex=67c48318&is=67c33198&hm=10c80f02d3ae95b2f77f797cd6cbc74af94a9caac8e4ec69b031b8af2c1eb564&
    3.  
Add Comment
Please, Sign In to add comment
Advertisement