How do i Keep Rotation While tweening CFrame

Hello!

I am working on tower defense game, and i ran into a problem.
Then im tweening zombies to their waypoint, its messing up Rotation of the Zombie

for i=1,#workspace.Map.Waypoints:GetChildren() do
		if model then
			if ExtraWaypoint ~= nil then
				if i ~= ExtraWaypoint and i < ExtraWaypoint then
					continue
				end	
			end
			
			local success, err = pcall(function()
				local waypoint = workspace.Map.Waypoints:FindFirstChild(i)
				local distance = (model.HumanoidRootPart.Position - waypoint.Position).Magnitude
				local Time = distance/humanoid.WalkSpeed

				local offset = Vector3.new(math.random(-200, 200)/200, 0, math.random(-200, 200)/200)
				local wantedCFrame = CFrame.new(Vector3.new(waypoint.Position.X,model.HumanoidRootPart.Position.Y,waypoint.Position.Z) + offset)
			
				local enemyCframe = CFrame.new(model.HumanoidRootPart.Position,Vector3.new(waypoint.Position.X,model.HumanoidRootPart.Position.Y,waypoint.Position.Z))
				model.HumanoidRootPart.CFrame = enemyCframe -- Setting correct rotation to the zombie, so it would look at the Waypoint

				local tween = TweenService:Create(model.HumanoidRootPart,TweenInfo.new(Time,Enum.EasingStyle.Linear),{CFrame = wantedCFrame})
				
				tween:Play() -- Tweening, that messes up Rotation
				model.Config.MovingToWaypoint.Value += 1

				wait(Time)
			end)
		end
	end
2 Likes

Is the zombie not turning towards the waypoint because of the tween the issue?

If so then try tweening the position instead of its cframe

1 Like

We need more info. Do you have videos, screenshots?
What exactly is wrong with the rotation?

Whatever details you can include would be great.

If you want to maintain orientation, you can just tween the position instead of cframe?

The problem is that TweenService is tweening the entire CFrame of the HumanoidRootPart, which includes both position and rotation. When you set the CFrame to wantedCFrame, it’s also setting the rotation to face the original position of the waypoint, not taking into account the offset.

To fix this, you can calculate the CFrame for the rotation after the offset has been applied.

local waypointPosition = Vector3.new(waypoint.Position.X,model.HumanoidRootPart.Position.Y,waypoint.Position.Z) + offset
local wantedCFrame = CFrame.new(waypointPosition)

local enemyCframe = CFrame.new(model.HumanoidRootPart.Position, waypointPosition)
model.HumanoidRootPart.CFrame = enemyCframe -- Setting correct rotation to the zombie, so it would look at the Waypoint

local tween = TweenService:Create(model.HumanoidRootPart,TweenInfo.new(Time,Enum.EasingStyle.Linear),{CFrame = wantedCFrame})

image
They should look towards the waypoint

Position would drag the rest of the body.

Position would drag the rest of the body. /

for i=1,#workspace.Map.Waypoints:GetChildren() do
		if model then
			if ExtraWaypoint ~= nil then
				if i ~= ExtraWaypoint and i < ExtraWaypoint then
					continue
				end	
			end
			
			local success, err = pcall(function()
				local waypoint = workspace.Map.Waypoints:FindFirstChild(i)
				local distance = (model.HumanoidRootPart.Position - waypoint.Position).Magnitude
				local Time = distance/humanoid.WalkSpeed

				local offset = Vector3.new(math.random(-200, 200)/200, 0, math.random(-200, 200)/200)
				local wantedCFrame = CFrame.new(Vector3.new(waypoint.Position.X,model.HumanoidRootPart.Position.Y,waypoint.Position.Z) + offset)
			
				local enemyCframe = CFrame.new(model.HumanoidRootPart.Position,Vector3.new(waypoint.Position.X,model.HumanoidRootPart.Position.Y,waypoint.Position.Z))
				model.HumanoidRootPart.CFrame = enemyCframe -- Setting correct rotation to the zombie, so it would look at the Waypoint

				local tween = TweenService:Create(model.HumanoidRootPart,TweenInfo.new(Time,Enum.EasingStyle.Linear),{CFrame = wantedCFrame})
				
				tween:Play() -- Tweening, that messes up Rotation
				model.Config.MovingToWaypoint.Value += 1

				wait(Time)
			end)
		end
	end

Still i dont understand

So, did it solve the problem though?

(Here is that script with edits):

for i=1,#workspace.Map.Waypoints:GetChildren() do
	if model then
		if ExtraWaypoint ~= nil then
			if i ~= ExtraWaypoint and i < ExtraWaypoint then
				continue
			end	
		end
		
		local success, err = pcall(function()
			local waypoint = workspace.Map.Waypoints:FindFirstChild(i)
			local distance = (model.HumanoidRootPart.Position - waypoint.Position).Magnitude
			local Time = distance/humanoid.WalkSpeed

			local offset = Vector3.new(math.random(-200, 200)/200, 0, math.random(-200, 200)/200)
			local waypointPosition = Vector3.new(waypoint.Position.X,model.HumanoidRootPart.Position.Y,waypoint.Position.Z) + offset
			local wantedCFrame = CFrame.new(waypointPosition) * CFrame.Angles(0, model.HumanoidRootPart.CFrame.Yaw, 0)
		
			local enemyCframe = CFrame.new(model.HumanoidRootPart.Position,Vector3.new(waypoint.Position.X,model.HumanoidRootPart.Position.Y,waypoint.Position.Z))
			model.HumanoidRootPart.CFrame = enemyCframe -- Setting correct rotation to the zombie, so it would look at the Waypoint

			local tween = TweenService:Create(model.HumanoidRootPart,TweenInfo.new(Time,Enum.EasingStyle.Linear),{CFrame = wantedCFrame})
			
			tween:Play() -- Tweening, that messes up Rotation
			model.Config.MovingToWaypoint.Value += 1

			wait(Time)
		end)
	end
end

Doesnt work. ​​​​​​​​​​​

Post must be at least 31 characters

Actually just found this tweet

1 Like

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