Tweening CFrame but keeping orientation the same

Hi, I am tweening my enemies to move them on the client but i am running into an issue. When i tween the humanoidrootpart of the enemy model the orientation is not consistent and it creates an effect where the enemy is slowly spinning to the correct orientation. I have been trying to find a solution for the past 2 hours but could not figure anything out. Any help would be appriecated.

local function TweenZombieModel(SentTable,ZombieModel)
	local NextWaypoint = workspace.Waypoints:FindFirstChild(tostring(SentTable.CodedVector.X))
	local CurrentWaypoint = workspace.Waypoints:FindFirstChild(tostring(tonumber(NextWaypoint.Name) - 1))
	if CurrentWaypoint == nil then
		ZombieModel.PrimaryPart.CFrame = workspace.Spawn.CFrame
		CurrentWaypoint = workspace.Spawn
	end
	local Distance = (CurrentWaypoint.Position - NextWaypoint.Position).Magnitude
	local Time = Distance / SentTable.CodedVector.Z
	local TInfo = TweenInfo.new(Time,Enum.EasingStyle.Linear)
	local PrimaryPart = ZombieModel.PrimaryPart
	local TargetCFrame = CFrame.new(NextWaypoint.CFrame.Position,-PrimaryPart.Position)
	local Goals = {
		CFrame = TargetCFrame,
	}
	local Tween = TS:Create(ZombieModel.PrimaryPart,TInfo,Goals)
	PrimaryPart.CFrame = CFrame.lookAt(PrimaryPart.Position,NextWaypoint.Position)
	Tween:Play()
	
	Tween.Completed:Connect(function()
		if tonumber(NextWaypoint.Name) == #workspace.Waypoints:GetChildren() then
			ZombieModel:Destroy()
		end
	end)
end

Set the Orientation manually and only tween the Position.

Unless your zombies are flying or moving through objects, the Humanoid:MoveTo command should work. That way, the enemies can move to your desired location without the need for tweening.

1 Like

Maybe Something like this, But I feel like this code would just be overcomplicating another Method

local OldCF = Model:GetPivot() -- Model CFrame
local x,y,z = OldCF.Rotation:ToOrientation() -- Rotation

Goal = {CFrame = newCF * CFrame.Angles(x, y, z)} -- Tween Goal

But, You can “Lerp” the Position of the Model to the Desired area, to do this, we would have to manually Interpolate its Position, which if you dont know the Formula for Lerp, its a + (b - a) * t, which is what we will use here, but in order to set the Position of the Model, we can use Model:MoveTo to apply a Position.

local function lerp(a, b, t) 
    -- a is your Current Position
    -- b is the Position you want to go to
    -- t is the time Position, which is between 0 and 1
    return a + (b - a) * t -- Should Return the Interpolated Amount using the formula
end
-- Below here is Applying a Interpolation to a Model
local function lerpModel(m: Model, p: Vector3, t: number) -- Arguments to Apply to the Model
    local currentPos = m:GetPivot().Position -- Gets the Position of the Model
    local lerpedPos = lerp(currentPos, p, t) -- Lerps with the given info
    m:MoveTo(lerpedPos) -- Lerps the Model with the interpolated Position
end
1 Like

If i tween only the position it will only move the humanoidrootpart

b is the next waypoints position right?
EDIT: never mind i saw you wrote what each thing is in the code itself

I found a solution.
Rather then tweening the model i did that every 5 frames i fired a remote to all clients containing the position the enemy needs to be in and just did PivotTo() to it

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