How to get rid of this weird orientation bug on a tweened NPC

Hello, I was wondering how I could get rid of this weird orientation bug for the first few rotations between these waypoints. Basically I have a for waypoint = 1, #waypoints:GetChildren() do loops that tween the NPC to the next waypoint and set the orientation to the waypoints lookvector. But the problem is that if I wanted to tween the orientation to smoothly look at the new waypoint then the NPC flips suddenly to be on the correct axis. Making it do what is in the video. It seems to work a little bit better if I get rid of all Orientation on the waypoint parts but that is kind of tedious to do it for say 20 waypoints in a big map. ( CFrame -> Orientation -> Vector3(0, 0, 0) ). Is there any other solution to this issue?

for waypoint = 1, #waypoints:GetChildren() do

	local Distance = (hrp.Position - waypoints[waypoint].Position).Magnitude

	local cframeLookat = CFrame.lookAt(hrp.Position, Vector3.new(waypoints[waypoint].Position.X, hrp.Position.Y, waypoints[waypoint].Position.Z))

	local rx, ry, rz = cframeLookat:ToOrientation()
	local dx, dy, dz = math.deg(rx), math.deg(ry), math.deg(rz)

	local orientation = Vector3.new(dx, dy, dz)

	local moveTween = TweenService:Create(hrp, TweenInfo.new(GetTime(Distance, script.Parent:FindFirstChild("WalkSpeed").Value), Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Position = waypoints[waypoint].Position}) 
	hrp.Orientation = orientation

	-- The old code used to Tween the orientation | TweenService:Create(hrp, TweenInfo.new(0.25, Enum.EasingStyle.Exponential), {Orientation = orientation}):Play() 

	moveTween:Play()

	moveTween.Completed:Wait()

end

TL;DR I want the NPC’s orientation to be like the last 2 seconds of the video while doing that smoothly.

Any help is appreciated!

1 Like

Its probably because its trying to look at the node while its inside of it, causing that weird spin

But I am not tweening the orientation in the above clip so what is making it do that?

my bad i though it was in a render stepped loop. Remove the cframlookat stuff (probably by commenting it out), and see if it happens still.

What do you mean exactly? I want it to smoothly turn to the next waypoint without doing that really quick spin.

you could just tween or lerp the corner. Or just use bezier curves. Most td games use them

What do you mean bezier curves? Do you have an idea of how I could implement them? Is that how TDS has a zombie system where they spawn at differently positioned waypoints and stay that way?

Have you tried waiting until the moveTween is completed before tweening?

Code:

for waypoint = 1, #waypoints:GetChildren() do
	local Distance = (hrp.Position - waypoints[waypoint].Position).Magnitude

	local cframeLookat = CFrame.lookAt(hrp.Position, Vector3.new(waypoints[waypoint].Position.X, hrp.Position.Y, waypoints[waypoint].Position.Z))

	local rx, ry, rz = cframeLookat:ToOrientation()
	local dx, dy, dz = math.deg(rx), math.deg(ry), math.deg(rz)

	local orientation = Vector3.new(dx, dy, dz)

	local moveTween = TweenService:Create(hrp, TweenInfo.new(GetTime(Distance, script.Parent:FindFirstChild("WalkSpeed").Value), Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Position = waypoints[waypoint].Position}) 
	moveTween:Play()
	moveTween.Completed:Wait()
	
	local orientationTween = TweenService:Create(hrp, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {Orientation = orientation})
	orientationTween:Play()
	orientationTween.Completed:Wait()
end

Thanks. This worked! All I had to do was change the order of the tweens.

1 Like

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