Soldier Falling into ground (Tower Defence Game)

For some reason my tower, whenever he starts looking towards the enemy falls into the ground, and I can’t think what might be causing it

image
image

This is the code I am using to make it look at the enemy

local function lookAtTarget(target)
		local TargetPos = target.HumanoidRootPart.Position
		local TowerPos = Troop.HumanoidRootPart.Position
		local EnemyLookPos = Vector3.new(TargetPos.X, TowerPos.Y, TargetPos.Z) --Vector 3 of where to aim at
		local PivotCFrame = CFrame.lookAt(TowerPos, EnemyLookPos) -- CFrame pointing at ^^^ Vector 3
		
		Troop:PivotTo(PivotCFrame) -- Sets tower's CFrame	
	end

Fixed the issue, in case anyone else down the line has the same problem, the correct code should be

		local TargetPos = target.HumanoidRootPart.Position
		local TowerPos = Troop.HumanoidRootPart.Position
		local AdjustedTowerPos = Vector3.new(TowerPos.X, TowerPos.Y+1.7, TowerPos.Z)
		local EnemyLookPos = Vector3.new(TargetPos.X, TowerPos.Y, TargetPos.Z) --Vector 3 of where to aim at
		local PivotCFrame = CFrame.lookAt(AdjustedTowerPos, EnemyLookPos) -- CFrame pointing at ^^^ Vector 3
		
		Troop.HumanoidRootPart:PivotTo(PivotCFrame) -- Sets tower's CFrame	
	end 

the change was that it is Troop.HumanoidRootPart and not Troop, as just Troop changes the models CFrame, whereas instead you should change the HumanoidRootPart which moves the rest of the model with it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.