Hello, i have been making a spawning tower system for my game and when i spawn a tower, the rig only goes half way above the ground even after i add hipheight etc.
Video: https://youtu.be/ipmon2odBnk
Here is my code:
local player = game.Players.LocalPlayer
local currentCamera = workspace.CurrentCamera
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local remote_Event = ReplicatedStorage:WaitForChild("SpawnTowerEvent")
local SendAnimClient = ReplicatedStorage:WaitForChild("SendAnimClient")
local SendAnimHit = ReplicatedStorage:WaitForChild("SendAnimHit")
local start_raycast = script.Parent:WaitForChild("Start_raycast")
local canPlace = true
local towerToSpawn = nil
local ShotAnim = script:WaitForChild("Animation2")
local Rigs = {}
local function Idle(rig)
local humanoid = rig:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")
local Loaded_Animation = Animator:LoadAnimation(script:WaitForChild("Animation"))
Loaded_Animation:Play()
end
local function Anim(rig)
local humanoid = rig:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")
local Loaded_Animation = Animator:LoadAnimation(script:WaitForChild("Animation2"))
Loaded_Animation:Play()
end
local function spawnTower(towerName)
if not towerToSpawn then
local foundTower = ReplicatedStorage:WaitForChild("Towers"):FindFirstChild(towerName)
if not foundTower then return end
local clonedRig = foundTower:Clone()
clonedRig.Parent = workspace
for _, v in pairs(clonedRig:GetChildren()) do
if v:IsA("Part") or v:IsA("BasePart") then
v.CollisionGroup = "NPCS"
end
end
towerToSpawn = clonedRig
end
end
local function getMouseRay(exclude)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = exclude
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local mousePos = UIS:GetMouseLocation()
local ray = currentCamera:ViewportPointToRay(mousePos.X, mousePos.Y)
return workspace:Raycast(ray.Origin, ray.Direction * 150, raycastParams)
end
local function StopPlacing(Position)
if towerToSpawn then
remote_Event:FireServer(Position, towerToSpawn.Name)
task.wait(.1)
if towerToSpawn and towerToSpawn.Parent then
towerToSpawn:Destroy()
end
towerToSpawn = nil
end
end
RunService.RenderStepped:Connect(function()
if towerToSpawn then
local result = getMouseRay({towerToSpawn})
if result and result.Position then
local halfHeight = towerToSpawn.Humanoid.HipHeight + (towerToSpawn.PrimaryPart.Size.Y / 2)
local x = result.Position.X
local y = result.Position.Y + halfHeight
local z = result.Position.Z
local cframe = CFrame.new(x,y,z)
towerToSpawn:SetPrimaryPartCFrame(cframe)
end
end
end)
start_raycast.Event:Connect(function(towerName)
if canPlace then
spawnTower(towerName)
canPlace = false
end
end)
UIS.InputBegan:Connect(function(input, processed)
if processed then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 and towerToSpawn then
local result = getMouseRay({towerToSpawn})
if result then
StopPlacing(result.Position)
canPlace = true
end
end
if input.KeyCode == Enum.KeyCode.T then
if canPlace then
spawnTower("DefaultTower")
canPlace = false
end
end
end)
SendAnimClient.OnClientEvent:Connect(function(rig)
Idle(rig)
end)
SendAnimHit.OnClientEvent:Connect(function(rig)
Anim(rig)
end)