Advertisement
Quoteory

Sphere2

Aug 8th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.14 KB | None | 0 0
  1. -- Sphere
  2. -- Quoteory
  3. -- August 8, 2019
  4.  
  5. --[[
  6.    
  7.  
  8. --]]
  9.  
  10.  
  11.  
  12. local Sphere = {}
  13.  
  14. local spherePart = Instance.new("Part")
  15. spherePart.Anchored = true
  16. spherePart.CastShadow = false
  17.  
  18. function inRadius(Mag, minRadius, maxRadius) -- returns if a Part is between the min and max radius, or if it's less than the min radius (inner sphere)
  19.     local targetRadius = (Mag >= minRadius and Mag <= maxRadius)
  20.     local innerRadius = Mag < minRadius
  21.    
  22.     if targetRadius then
  23.         return "targetRadius"
  24.     elseif innerRadius then
  25.         return true
  26.     end
  27.    
  28. end
  29.  
  30. function newSpherePart(Size, CF, Trans, Color, Parent)
  31.    
  32.     local newPart = spherePart:Clone()
  33.     newPart.Size = Size
  34.     newPart.CFrame = CF
  35.     newPart.Transparency = Trans
  36.     newPart.BrickColor = Color
  37.     newPart.Parent = Parent
  38.    
  39.     return newPart 
  40.    
  41. end
  42.  
  43. function Sphere:Create(Radius, PartSize, Parts, Position)
  44.    
  45.     local Parts = Parts/3 -- amount of parts that shape will be
  46.     local centerCFrame = CFrame.new(Position)
  47.     local Size = Vector3.new(PartSize, PartSize, PartSize) -- the size of the sphere parts
  48.     local Width = PartSize * Parts -- width of the presphere (like a cube)
  49.  
  50.     -- this generates parts in a cube and detects if they are within a certain radius and does stuff based on that
  51.     for x = 1, Parts do
  52.         for y = 1, Parts do
  53.             for z = 1, Parts do
  54.                
  55.                 local Offset = CFrame.new(-(Width/2)-PartSize/2, -(Width/2)-PartSize/2, -(Width/2)-PartSize/2)
  56.                 local CF = centerCFrame * CFrame.new(PartSize * x, PartSize*y, PartSize * z) * Offset
  57.            
  58.                 local minRadius, maxRadius = Radius - PartSize, Radius + PartSize
  59.                 -- min and max radius, think of it like the spheres outer shell
  60.                
  61.                 local isInRadius = inRadius((centerCFrame.Position - CF.Position).magnitude, minRadius, maxRadius)
  62.                 -- calls inRadius() and gets a return value
  63.                
  64.                 if isInRadius then -- doesn't spawn a part if it's not within radius
  65.                    
  66.                     if isInRadius == "targetRadius" then
  67.                         newSpherePart(Size, CF, 0, BrickColor.Red(), workspace)
  68.                     else
  69.                         newSpherePart(Size, CF, 0, BrickColor.Gray(), workspace)           
  70.                     end
  71.                    
  72.                 end
  73.                    
  74.             end
  75.         end
  76.     end
  77.    
  78. end
  79.  
  80.  
  81.  
  82. return Sphere
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement