How would I make this NPC look at me while walking?

I got an NPC that follows you but doesn’t wanna stay too close, the system works but I’d like the NPC to look at the player meanwhile it backs up

If this helps, the pathfinding is done inside a while loop

I tried doing some weird math with CFrame.new(), and although it did work, if I do it meanwhile the npc is moving, the npc just keeps on stuttering

Showcase:

13 Likes

Have you tried something like this:

local npcRoot 
local plrRoot  

game:GetService("RunService").Heartbeat:Connect(function()
      local cfRot = CFrame.new(npcRoot.Position, plrRoot.Position).Rotation
      npcRoot.CFrame *= cfRot 
end)

This would make the primary part of the NPC face towards the player’s primary part. I’m not sure if it’ll prevent the Humanoid instance from automatically rotating but you can try it.

7 Likes

Hmm, didn’t quite work…

5 Likes

Oh sorry I multiplied the two cframes, this should be good:

local cfRot = CFrame.new(npcRoot.Position, plrRoot.Position)
npcRoot.CFrame = cfRot 
2 Likes

That actually worked… but it has trouble moving now

5 Likes

If you’re using :MoveTo() something could be anchored, or there could be another problem. You can try removing the code I gave but I don’t think it’ll matter cause it’s only for rotation.

Once your NPC is moving just change the CFrame of the npc’s root part once in a while to make it face the player

edit: I was scrolling and the Humanoid has a property called AutoRotate, try turning that off and just change the cf of the npc root part once in a while

3 Likes

It’s already off, nothing is anchored, im using SimplePath module

4 Likes

I don’t really know. If you made edits to your pathfinding code or whatever then have a quick look again. It won’t have nothing to do with humanoids because your npc was fine moving backwards.

4 Likes

Or, you could use AlignOrientation. It locks the NPC to a certain rotation, whilst still allowing it to move around freely. It acts like a CFrame, except it only uses the rotational values of the CFrame.

Here’s how you would use it for this instance:

-- RootPart is a child of the NPC.
-- We're also assuming the AlignOrientation is already created and set up.

RunService.Heartbeat:Connect(function()
    local diff = PlayerPos - RootPart.Position
    local angle = math.atan2(diff.X, diff.Z) -- we just want the RootPart to orientate on one axis.

    local AlignOrientation = RootPart:FindFirstChild("AlignOrientation")
    AlignOrientation.CFrame = CFrame.Angles(0, angle, 0)
end)

If you need more information on setting this up, you should click on the website linked to the instance in question. Let us know how it goes.

7 Likes

Been messing around with it for 30 mins now, I can’t get it to work properly :sob:

3 Likes

AlignOrientation works only if there’s attachment inside one part and assigned from a property, You have to set Responsiveness to 200 and the MaxTorque higher to make it rotate properly

id recommend setting from TwoAttachment to OneAttachment so it only uses one part to look at player

you can still use CFrame.lookat on AlignOrientation just like setting a part to look at but it won’t get updated unless it’s in a loop or RunService

6 Likes

Nevermind it worked, I just had to reverse the “diff” variable and also use math.atan2

I didn’t wanna use that because I just don’t know what it does, but it’s whatever now.

4 Likes

Maybe I should’ve also explained how this worked and how you should’ve set this up. … I apologize.

math.atan2 is simply just a function that uses trigonometry to get a radian angle from the direction made up by x and y, typically used for getting the angle orientation of an axis without doing too much work. Just think of it as finding the angle from a LookVector, except its space is within the world.

Probably could be explained using this image:

My code above could’ve been better if diff was turned into a unit, which would made this example much easier to understand because the unit always results in a magnitude of 1. But, the idea is that you’re getting the angle from tan0, which is the quotient of sin0 / cos0, which x and y represents.

Like, for example, if x = 1 and y = 0, then the angle produced by math.atan2 would be pi * 2, or 0 degrees. If it were the opposite, (x = -1; y = 0) then it would just be pi, or 180 degrees. (Free note: pi = 180 deg) One more example is if x = 0.5; y = 0.5), then it would give us pi / 4, or 45 degrees. The negated version would’ve given us 135 degrees.

There’s this video that explained this better than I did, but that was my attempt at trying to give a simplified way of looking at math.atan2’s functionality. That aside, glad that you got it to working. I wish you the best of luck in your project.

12 Likes

aneurysm-hhhhomeboyyyy

CHILL BRO I DIDN’T APPROVE HIGH SCHOOL

7 Likes

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