I am currently working on a tower defense game and I recently completed the first map for the game. Before I was using Humanoid:MoveTo() to move the humanoid along the map’s path, but stretches of the course are too long, so the MoveTo times out before reaching the next segment. So, as a result I moved to using tweening to move my enemy models to the end of the track. This works great, except for one thing. Whenever I tween the HumanoidRootPart of my enemies, the orientation slowly resets itself back to 0 during the tween.
My issue is that, while tweening my enemies to along my path, their orientation is slowly reset to 0 during the tween. Is there anyway I can stop this? I am not sure of what to do or how to do it.
Here is a video that shows them slowly rotating (Sorry about how short it is):
Here is the code for my tween:
for i = 1, (#trackParts:GetChildren() - 1), 1 do
local trackPart = trackParts[i]
local previousValue = i - 1
local enemyPos = enemyClone.PrimaryPart.Position
local trackPartPosition = trackPart.Position
local newCFrame = CFrame.new(enemyPos, trackPartPosition)
enemyClone.HumanoidRootPart.BodyGyro.CFrame = newCFrame
enemyClone:SetPrimaryPartCFrame(newCFrame)
local endPoint = {
CFrame = CFrame.new(trackPart.Position)
}
local tweenInfo = TweenInfo.new(((((trackParts:FindFirstChild(i).Position - trackParts:FindFirstChild(previousValue).Position).Magnitude) / 7.5) * enemyClone.Humanoid.WalkSpeed), Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
local tween = tweenService:Create(enemyClone.HumanoidRootPart, tweenInfo, endPoint)
tween:Play()
tween.Completed:Wait(0.1)
end
If anyone knows how to stop the orientation from resetting the tween that would be really helpful! I have already tried using a BodyGyro but I am not familiar enough with them to know if I set it up properly.
Inside the tween, make it a dictionary right after tweenInfo with {}. Then just set the rotation CFrame to the current CFrame of the zombie. So it is setting to itself the whole time. I am almost sure that would work.
for i = 1, (#trackParts:GetChildren() - 1), 1 do
local trackPart = trackParts[i]
local previousValue = i - 1
local enemyPos = enemyClone.PrimaryPart.Position
local trackPartPosition = trackPart.Position
local newCFrame = CFrame.new(enemyPos, trackPartPosition)
enemyClone.HumanoidRootPart.BodyGyro.CFrame = newCFrame
enemyClone:SetPrimaryPartCFrame(newCFrame)
local endPoint = {
CFrame = trackPart.CFrame
}
local tweenInfo = TweenInfo.new(((((trackParts:FindFirstChild(i).Position - trackParts:FindFirstChild(previousValue).Position).Magnitude) / 7.5) * enemyClone.Humanoid.WalkSpeed), Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
local tween = tweenService:Create(enemyClone.HumanoidRootPart, tweenInfo, endPoint)
tween:Play()
tween.Completed:Wait(0.1)
end
The HumanoidRootPart still rotates during the tween. Could this have anything to do with the orientation or another property of the part I am tween the enemies to?
Edit: All of the track parts I tween the enemies to have an orientation of 0 on each axis.
CFrame values include position and rotation. If your creating a CFrame with just position, then it’s orientation will get reset. You can create CFrame angles with CFrame.Angles(x, y, z) in radians.
Sorry for the pretty late reply. I ended up figuring out that was the issue so I ended up tweening with position instead of CFrame. I’ll mark you as solution though since you mentioned the part that was causing the issue.