I am making a tower defense game, and I ran into this issue. The script is seeing the right position where my mouse is pointing, but where the actual tower is placed is slightly off. It is slightly too far ahead and it amplifies the farther out I go. I wish I could have uploaded files, but this isn't Roblox devforumn.
Code:
local function place()
print("func fired")
local newTower = tower:Clone()
local bubble = newTower.Bubble
local range = newTower.Settings.Range.Value
local account = 16
bubble.Size = Vector3.new(0.5, range + account, range + account)
bubble.Transparency = 0.6
bubble.Material = Enum.Material.Neon
local yOrientation = 0
local canPlace = false
if not placing then
placing = true
newTower.Parent = workspace
local startCFrame = CFrame.new(0, -2, -15)
newTower:SetPrimaryPartCFrame(root.CFrame:ToWorldSpace(startCFrame))
game:GetService("RunService").RenderStepped:Connect(function()
if newTower and placing then
local mouseRay = mouse.UnitRay
local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000000000)
local ignoreList = newTower:GetChildren()
table.insert(ignoreList, #ignoreList, root)
local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)
if hit then
if hit.Name == "Grass" and hit.Parent.Name == "FloorTowers" then
canPlace = true
bubble.Color = Color3.fromRGB(0, 160, 255)
else
canPlace = false
bubble.Color = Color3.fromRGB(255, 0, 0)
end
if newTower:FindFirstChild("Bubble") then
newTower.Bubble.Transparency = 0.6
end
local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
local newCFrame = CFrame.new(position.X, position.Y + yOffset, position.Z)
if newTower then
if newTower.PrimaryPart then
newTower:SetPrimaryPartCFrame(newCFrame * newCFrame)
end
end
end
end
end)
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
rKeyDown = true
local speed = 2.5
while rKeyDown do
yOrientation = yOrientation + speed
tower.PrimaryPart.Orientation = Vector3.new(0, yOrientation, 0)
wait()
end
end
end)
uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
rKeyDown = false
end
end)
mouse.Button1Down:Connect(function()
if placing then
if canPlace and newTower then
game.ReplicatedStorage.Remotes.PlaceTower:FireServer(tower, newTower.PrimaryPart.CFrame)
game.ReplicatedStorage.Remotes.PlaceTower.OnClientEvent:Wait()
newTower:Destroy()
placing = false
end
end
end)
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Z then
placing = false
newTower:Destroy()
end
end)
end
end
It's on a local script and it works just fine except for where the tower itself is being placed. Thanks for your help!
