Making an NPC face the player by changing RootJoint C0

It works rotating the character but it also makes them jitter and barely move. I think I should also mention that the rotation function is connected to a Heartbeat event.

ur in a local script or a server script? if you’re in a server script u can either use a while loop, or a runservice

I’m running this in a serverscript. And wouldn’t while loop cause the same issue? From what I understand the jittering and slow movement stems form the CFrame getting updated too fast

I’ve tried updating CFrame after the NPC reached a waypoint but that made it look like it’s teleporting instead

can i see ur whole npc moving script thing? so i could see what’s happening behind the code

local function FollowPlayer(Player)
	
	Path:ComputeAsync(Character.PrimaryPart.Position, Player.PrimaryPart.Position)

	if Path.Status == Enum.PathStatus.Success then

		Waypoints = Path:GetWaypoints()

		local TargetWaypoint = math.clamp(#Waypoints,0,3)

		if TargetWaypoint <= 1 then return end
			
			if not Player or Player.Humanoid.Health <= 0 then return end
			

			if Waypoints[TargetWaypoint].Action == Enum.PathWaypointAction.Jump then
				TargetWaypoint = math.clamp(TargetWaypoint+2,1,#Waypoints)
				Hum.Jump = true
				Hum:MoveTo(Waypoints[TargetWaypoint].Position)
			else
				Hum:MoveTo(Waypoints[TargetWaypoint].Position)
			end
			
			local Reached = false
			local Break = false

			BlockedConnection = Path.Blocked:Connect(function()
				BlockedConnection:Disconnect()
				Break = true
				return
			end)

			ReachedConnection = Hum.MoveToFinished:Connect(function()
				ReachedConnection:Disconnect()
				Reached = true
			end)

			task.delay(.2,function()
				if not Reached and not Break then
					Break = true
				end
			end)
			
			repeat task.wait() until Reached or Break

	end

end

And this code is connected to a while true do loop

seems to me that you’re using pathfinding
about that, i did find a post, but again i might be wrong bout this, anyway here is the post: Pathfinding NPC stuttering

its basically talking about a npc stuttering/jittering

edit: after more researching, it might be a network issue, just make sure u set the network in the code

The pathfinding isn’t what’s causing the jittering, it’s the code you suggested to make the NPC rotate towards the player, as it sets CFrame too fast not letting the character move. That’s why I was asking for help with setting C0 of the RootJoint instead of CFrame

well when the enemy chases the player shouldnt it just like auto rotate

Autorotate causes the NPC to face the direction it’s moving, not the player. Plus I want to make it so the NPC looks at the player even if they are out of reach

tho i got one question, is there just 1 player in the game or multiple?

There’s gonna be multiple players in the game

so basically what you want to do is to chase the nearest player? while also looking at him, from far away right?

1 Like

do u want the enemy to look at its current target for a little amount of seconds and then chase him?

btw, im asking theese questions so i can know how i can just fix the issue

No I’m trying to make the NPC look at target throughout, and while the player is out of reach

i do have a question, did u write also this piece of line?

Character.PrimaryPart:SetNetworkOwner(nil)

No I didn’t, what does the line do?

well on a few chase scripts i usually saw this line being put, heres a more clear documentation on it

it does something with lag or i dont really know… i also need to have a better research on it
it might not be needed but i just wanted to point this out

edit: i also found this post (might not match ur solution):

also one more thing in ur case, which u want the enemy to look at the target even if its far away, you’d probally have to do something like raycasting, or just check if the distances are too far and then make the enemy lookat the target and then, just chase it

(i am still not clear, of what you want)

So I found a way to ALMOST fix it, I’m just stuck at what I think should be the final part. I’ve made the rotate towards a certain point by offsetting the C0. However I’ve ran into another issue, if the character model’s rotation is not 0,0,0 then it will be offset by the Y value of the model’s rotation. Any ideas on how I can fix this?

My code:

    script.Parent.HumanoidRootPart.RootJoint.C0 = CFrame.new(Vector3.new(0,0,0),CFrame.lookAt(Pos.Position,Vector3.new(Target.X,Pos.Position.Y,Target.Z)).LookVector)
	script.Parent.HumanoidRootPart.RootJoint.C1 = CFrame.new(Vector3.new(0,0,0),Vector3.new(0,0,0))

Okay nevermind I fixed it myself, here’s the code for those who may run into the same issue:

    script.Parent.HumanoidRootPart.RootJoint.C0 = CFrame.new(Vector3.new(0,0,0),CFrame.lookAt(Pos.Position,Vector3.new(Target.X,Pos.Position.Y,Target.Z)).LookVector)*CFrame.Angles(math.rad(Rot.X),math.rad(Rot.Y),math.rad(Rot.Z)):Inverse()
	script.Parent.HumanoidRootPart.RootJoint.C1 = CFrame.new(Vector3.new(0,0,0),Vector3.new(0,0,0))

Rot is rotation of primary part
Pos is CFrame of primary part
Target is where you want character to look

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