add range indicator when placing towers

This commit is contained in:
JISAUAY committed 2025-07-25 14:42:50 -05:00
1 parent 4e29ad0824
commit a7ce7f9796
1 file changed
+54 -1
+54 -1
View File
@@ -1,4 +1,6 @@
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Tower = require(script.Parent.Tower)
local Waves = require(script.Parent.Waves)
@@ -46,6 +48,8 @@ local waypoints = { -- Fill with your waypoint Parts
local selectedTowerType = towerTypes[1]
local enemies = {} -- Table to hold TrackEnemy objects
local towers = {}
local placingTower = false
local rangeIndicator = nil
function selectTowerType(index)
selectedTowerType = towerTypes[index]
@@ -79,9 +83,57 @@ local function isOnTrack(position)
return false
end
local function showRangeIndicator(position, range)
if not rangeIndicator then
rangeIndicator = Instance.new("Part")
rangeIndicator.Name = "RangeIndicator"
rangeIndicator.Shape = Enum.PartType.Cylinder
rangeIndicator.Anchored = true
rangeIndicator.CanCollide = false
rangeIndicator.Transparency = 0.6
rangeIndicator.Color = Color3.fromRGB(100, 200, 255)
rangeIndicator.Material = Enum.Material.Neon
rangeIndicator.Size = Vector3.new(range * 2, 0.2, range * 2)
rangeIndicator.Parent = workspace
-- Rotate so cylinder is flat
rangeIndicator.Orientation = Vector3.new(0, 0, 90)
end
rangeIndicator.Size = Vector3.new(range * 2, 0.2, range * 2)
rangeIndicator.Position = Vector3.new(position.X, position.Y + 0.1, position.Z)
rangeIndicator.Visible = true
end
local function hideRangeIndicator()
if rangeIndicator then
rangeIndicator:Destroy()
rangeIndicator = nil
end
end
RunService.RenderStepped:Connect(function ()
if placingTower then
local mouse = game.Players.LocalPlayer:GetMouse()
local position = mouse.Hit.p
showRangeIndicator(position, selectedTowerType.range)
else
hideRangeIndicator()
end
end)
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if input.KeyCode == Enum.KeyCode.T then
placingTower = not placingTower
print("Tower Placing Mode:", placingTower and "ON" or "OFF")
if not placingTower then
hideRangeIndicator()
end
end
end)
UserInputService.InputBegan:Connect(function (input, gameProcessed)
if gameProcessed then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 and placingTower then
local mouse = game.Players.LocalPlayer():GetMouse()
local position = mouse.Hit.p
if not isOnTrack(position) then
@@ -94,6 +146,7 @@ UserInputService.InputBegan:Connect(function (input, gameProcessed)
selectedTowerType.effect
)
table.insert(towers, tower)
hideRangeIndicator()
end
end
end)