Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a NPC look at the character?

Asked by 5 years ago

I want to make a NPC move his body in the direction of the player's character when the player begins a dialog with the NPC. I saw this in a Roblox game called Monster Islands. Can somebody guide me?

1 answer

Log in to vote
0
Answered by
Divistern 127
5 years ago
Edited 5 years ago

Ok so i've thought about it, and i just made it check if the player is close to the selected npc or not, and it turned out to be like this; This is in a Script that is located in the StarterCharacterScripts

First: Getting needed Variables local char = script.Parent local npcToFind = workspace:WaitForChild("StoreClerk")

After that we will need to calculate distance in a loop.

Second: Getting distance between player/NPC spawn(function() while true do local TO = char.Torso local dist = (TO.Position - npcToFind.Torso.Position).magnitude wait() end end

Now that we have the distance we can just do certain events if the player is close or not.

Third: Checking if close or not, and if player is close, make npc look towards the player, if not then continue the loop. if dist <= 10 then npcToFind.Torso.CFrame = CFrame.new(npcToFind.Torso.Position, char.Torso.Position * Vector3.new(1,0,1) + char.Torso.Position * Vector3.new(0,1,0)) end


Full Code: ``` local char = script.Parent local npcToFind = workspace.StoreClerk

spawn(function() while true do local TO = char.Torso local dist = (TO.Position - npcToFind.Torso.Position).magnitude if dist <= 10 then npcToFind.Torso.CFrame = CFrame.new(npcToFind.Torso.Position, char.Torso.PositionVector3.new(1,0,1) + char.Torso.PositionVector3.new(0, 1, 0)) end wait() end end) ```

Results:
Gif Of the results Images: !enter image description here !enter image description here And by all of that, the NPC Will directly look at the player if the player is far away from the npc by 10 Studs you can always change that number, and if you want it to be a lot of npcs, you can just put the npcs in a list and constantly looping through that list.

Hope This Helped.

Ad

Answer this question