Making an NPC face the player by changing RootJoint C0

Hello! I’m currently making enemy AI for my game and I’m trying to make it so the NPC always faces the player it is chasing. Setting HRP CFrame to look at player kinda works but it makes it look like the NPC is teleporting. So the only other way I could think of is changing the C0.

Though my attempts at that result in this happening
image

my code

Character.HumanoidRootPart.RootJoint.C0 = CFrame.new(BodyC0.Position,(Target.PrimaryPart.Position+Character.PrimaryPart.Position*Vector3.new(0,1,0)))
3 Likes

well did u try to use cframe.lookat()?

image

Yeah, same issue, it just does whatever this is

can i see how u used the cframe.lookat()? the enemy should not be like that

Setting CFrame would just stop character from moving so I edit C0. There’s a high chance I’m just using lookAt wrong. BodyC0 is the default value of RootJoint.C0

Character.HumanoidRootPart.RootJoint.C0 = CFrame.lookAt(BodyC0.Position,Target.PrimaryPart.Position+Character.PrimaryPart.Position*Vector3.new(0,1,0))

hmm, i dont think CFrame should stop the enemy from moving

try this maybe:

Character.HumanoidRootPart.CFrame=CFrame.lookAt(Character.HumanoidRootPart.Position, Target.HumanoidRootPart.Position)

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