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
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})
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
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