I want my tower in my TD game to look at the enemy when attacking.
Targeting is correct, there is no animation.
My Tower script is a module script. Face target, attack functions are also placed here.
A function that makes the tower look at the enemy:
function tower.FaceTarget(newTower, target, duration) -- Aim tower's function
local targetVector = Vector3.new(target.PrimaryPart.Position.X, newTower.PrimaryPart.Position.Y, target.PrimaryPart.Position.Z)
local targetCFrame = CFrame.new(newTower.PrimaryPart.Position, targetVector)
local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
local faceTargetTween = TweenService:Create(newTower.PrimaryPart, tweenInfo, {CFrame = targetCFrame})
faceTargetTween:Play()
faceTargetTween.Completed:Connect(function(playbackState)
print(tostring(playbackState))
end)
print(newTower, target, duration)
end
My facing function fires in the attack function:
tower.FaceTarget(newTower, target, 0.06) --last number - duration during tower aiming
I tried to do this using Tween, nothing happens and there are no errors.
If I set duration to near 0 and I place tower and enemy is in its range, placed tower will look at enemy but then it won’t move anymore. Tower attacks, dealing damage, etc. work properly.
I used to use BodyGyro, but I ran into a problem: when I set up a tower and it needed to turn quickly to face the enemy, the tower would spin up and start moving uncontrollably around the map until it stopped. So I decided that I needed a smoother aiming system where I could control the duration of the turn.
BodyGyro is off. All data transferred correctly to FaceTarget, I used prints to check. Also, as I found out, Tween probably works too, to check it I used:
faceTargetTween.Completed:Connect(function(playbackState)
print(tostring(playbackState))
end)
I tried to put FaceTarget function into a local script into a part and it worked:
I would appreciate any help because I’ve already spent 2 months and still haven’t found the solution.