CFrame based NPC walking going crazy

Try this:

for i, v in ipairs(path:GetWaypoints()) do
	
	--Distance to goal
	local dist = (restockerNPCHum.RootPart.Position - v.Position).Magnitude
	dist = dist - restockerNPCSize.Z/2 --Dist minus thickness of character

	--Time
	local calculatedTime = (dist/restockerNPCHum.WalkSpeed)
	
	local currPos = restockerNPC:GetPivot().Position
	local goalPos = v.Position
	
	--Vector Direction looking toward our goal position from current pos (ignoring Y axis)
	local lookVector = CFrame.lookAt(currPos, v.Position).LookVector * Vector3.new(1,0,1) 
	
	--Position 10 units forward of goal position relative to character lookVector
	local lookAt = v.Position + (lookVector * -10)
	
	--Final CFrame to tween to
	local calculatedCFrame = CFrame.lookAt(v.Position, lookAt)
	
	local tween = tweenService:Create(restockerNPCHum.RootPart, TweenInfo.new(calculatedTime, Enum.EasingStyle.Linear), {CFrame = calculatedCFrame})
	tween:Play()
	tween.Completed:Wait()
end
1 Like

image

I have no idea how your getting those values.

Me neither, I am beyond confused. Iā€™ll be opting for lerping instead.

Thatā€™s origin Position not current position

When I press F on it, It takes me to this void. Even if I donā€™t zoom in on it, I donā€™t see the NPC itā€™s justā€¦ Gone!

Change this part to:

local currPos = restockerNPCHum.RootPart.Position
1 Like

The NPC got poofed, Apparently you canā€™t tween humanoids using CFrame (position + angle).

Sure you can. There is something else happening.

Iā€™m gonna try CFrame:ToWorldSpace()

local calculatedCFrame = CFrame.new(v.Position):ToWorldSpace(lookAt)

Whooooops! I did something wrong here.

Aw manā€¦ It still gets sent to an alternate timeline!

try commenting out the tween:Play and wait lines totally then run your code. See what happens.

Thereā€™s something wrong with the calculationsā€¦

1 Like

Did you run the code with the tween lines commented out?

Yeah, I replaced it with root.CFrame = calculatedCFrame

no donā€™t replace it. We need to see if there is something else effecting the movement. So we want that loop to not do anything at all.

I replaced it after i ran it with the tween lines commented out. And there was nothing affecting the movement.

So it only goes to crazy when the tween runs? If the tween doesnā€™t run, then the character stays at the start position?