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

How to make a model/npc face the player?

Asked by 8 years ago

I'm going to try to explain my problem the best I can.

There's one player in the game, and only one player. This is a single player game, so don't worry about 2 or more players. I want a npc model to face the character, so for example if the player moves to the left of the npc, the npc will turn to the left to face the character. However, I don't want the npc to turn upwards to face the player, for example if the player were to jump on top of the npc, I don't want the npc to rotate on to its back to face the player.

I've been trying some lookvectorand cframe magic, but it didn't work too well. I've gotten somewhere with free models but their help was limited.

1 answer

Log in to vote
0
Answered by 8 years ago

The best way I can think of doing this is to use a particular CFrame constructor which looks like this: CFrame.new(Vector3 position, Vector3 point)

You could first set the PrimaryPart of the model to something that makes sense, such as the head or torso. Then, on a loop, use the SetPrimaryPartCFrame() function of Model, like so:

-- This assumes there is a variable called Character (the player's character) and Model (the model to rotate).

while true do
    Model:SetPrimaryPartCFrame(CFrame.new(Model.PrimaryPart.Position, Player.Torso.Position))
end

Then, to solve the problem of the NPC facing up/down, make a blank CFrame with the NPC's position, plus the Y rotation of the calculation we made earlier:

-- This assumes there is a variable called Character (the player's character) and Model (the model to rotate).

while true do
    local _, Y, _ = CFrame.new(Model.PrimaryPart.Position, Player.Torso.Position):toEulerAnglesXYZ()
    Model:SetPrimaryPartCFrame(CFrame.new(Model.PrimaryPart.Position) * CFrame.Angles(0, Y, 0))
end

Unfortunately, I can't guarantee this will work properly, since I haven't tested it or used CFrames often, but I hope it offers a bit of help at least.

Ad

Answer this question