Help Creating a Smooth Turning NPC

In simple terms, I need a sort of “Free Roaming” NPC that can walk around the place between waypoints without too much trouble.

The problem I have is that I can’t work out how to make the NPC turn smoothly.

robloxapp-20240414-1909028.wmv (1.7 MB)

(I don’t know if that video will appear as a preview, sorry if not)

So far I’ve tried using a BodyGyro to add smooth turning, but it more just makes it look like delayed turning, as it walks in 1 direction, and slowly turns towards the direction.

I have also tried Pathfinding, but this either comes out as super laggy and gittery, or doesn’t turn smoothly

Code currently:

local function actuallyMoveTheNPC(npc, waypoints, waypoint, Humanoid, BodyGyro)
	local waypoint = waypoints[waypoint]
	
	local path = PathfindingService:CreatePath({
		AgentRadius = 2,
		AgentHeight = 5,
		AgentCanJump = false,
		AgentCanClimb = true,
		WaypointSpacing = 1
	})
	path:ComputeAsync(npc.HumanoidRootPart.Position, waypoint.Position)
	
	local AssemblyMass = npc.HumanoidRootPart.AssemblyMass*400
	BodyGyro.MaxTorque = Vector3.new(AssemblyMass, AssemblyMass, AssemblyMass)
	
	for index, waypoint in path:GetWaypoints() do
		-- Move
		BodyGyro.CFrame = CFrame.new(npc.HumanoidRootPart.Position, Vector3.new(waypoint.Position.X, npc.HumanoidRootPart.Position.Y, waypoint.Position.Z))

		repeat Humanoid:MoveTo(waypoint.Position)
			local reached = Humanoid.MoveToFinished:Wait() 
		until reached
	end
end

Any help is appreciated!

Try tweenservice because it’s easy and efficient, however may not be 100% to your liking. Depending on the what it’s tweening. Try it out and see how you feel about it, if you don’t like it I’ll help you figure out another way!

What to do: find the part to rotate. If it’s a NPC I’m gonna assume it has a HumanoidRootPart, simply tween the CFrame’s angle by the degree the NPC is turning to.

1 Like

in additional OP might want to wait at each turning point for a specified time

I agree, it will improve the smoothness of the turning and should also decrease lag.

1 Like

Consider using AlignOrientation rather than BodyGyro. You can then increase the responsiveness to make the NPC rotate more quickly. There’s also the property RigidityEnabled that would make the NPC turn as quickly as possible towards the specified CFrame.

Thing is with TweenService, it isn’t always 100% accurate, and its fairly dodgy when it comes to tweening characters that are already moving.

Yeah, but how could I make it already start turning before it hits the waypoint, I want it to look as human-like as I can get it.

I’m not sure what you want to achieve then. If the problem is them turning while moving just have a new function or just put a wait timer.

I basically need an NPC to move to waypoints using PathfindingService. However instead of it going into the waypoint, snapping, then walking again, I want it to slowly turn as it approaches the waypoint, so it looks more life-like.

I use AlignOrientations for these. All my NPCs have an invisible ‘Looker’ part that’s used as the dominant attachment of the AlignOrientation. When the NPC needs to update where its looking, the invisible part is changed to face the target direction, with the AO following shortly after. This also helps the NPC keep facing a direction if they’re pushed around by things like LinearVelocities.

You can also change the responsiveness to set how quickly the NPC turns to face the direction.

How would this work with Humanoids/HumanoidRootParts that have NPC behaviour?

For anyone coming back to this post in the future, SimpleSpline works insanely well. If you combine this with AlignOrientation, and a separate part, you can get some really nice results.

2 Likes

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