How Can I Stop Rotation Resetting During a Tween?

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.

1 Like

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.

1 Like

I believe that you are resetting the position with CFrame.new(). Do this instead.

local endPoint = {
     CFrame = trackPart.CFrame
}

Ah yes, what CheekySquid said should work better and less code will be used.

1 Like

It still rotates the enemies during the tween when I changed it to that.

Could you send me the new code?

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

That was the only part I changed

I would try what @Coburncode did. I can’t think of why immediately, and the CFrame constructor that used a lookVector was removed I believe.

Try setting the direction directly based on the zombie’s and track’s positions.

	local endPoint = {
		CFrame = CFrame.new(
			trackPart.Position,
			(newFrame.Position-trackPart.Position) -- might need to invert this
		)
	}

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.

I ended up solving my problem by tweening to the track part position instead of the cframe. Thanks to those who gave me potential solutions though!

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.