Making a model face another model

I want to make an NPC face a target part I have defined by my script

It’s kind of rotating in any direction left or right, not in the correct way. And detecting if the NPC needs to rotate left or right isnt working either. The code prints so I know it’s running

Here is the script, which runs inside of a RunService.Heartbeat loop:

local GoalLookVector = (Target.Value:GetPivot().Position - SwatPivot.Position).Unit
local CurrentLookVector = SwatPivot.LookVector

local dir = GoalLookVector - CurrentLookVector

if GoalLookVector.X < CurrentLookVector.X then
	print("left")
	Rotate.AngularVelocity = Vector3.new(0, dir, 0)
else
	Rotate.AngularVelocity = Vector3.new(0, -dir, 0)
	print("right")
end

Edit: The humanoid’s AutoRotate property is set to false when the AngularVelocity is enabled

You can use CFrame.lookAt. You’ve left out the variables that are needed though like the actual swat model so I can’t fix it yet.

It needs to be physics based, since the swat will be walking around while facing the object. Another reason CFrame.lookAt is not an option, I only want the swat to be rotating left and right, while CFrame.lookAt may make him rotate around the other 2 axis as well

If it matters though, the swat variable is just called “Swat” and “Target” is an object value, where “Target.Value” is equal to the target model the swat needs to be facing

1 Like

Have you tried comparing LookVector.Z instead?

I have, and the same thing happened. When printing the lookVector of the swat NPC, even if I’ve only rotated the NPC left or right, both the X and Z values of the lookVector change

This weird math that chatgpt gave me seems to work if I set the Y value of the AngularVelocity’s rotation to the angle variable

-- Get the angle between the current and goal look vectors
local angle = math.atan2(GoalLookVector.X, GoalLookVector.Z) - math.atan2(SwatPivot.LookVector.X, SwatPivot.LookVector.Z)

-- Adjust the angle to be in the range of -pi to pi
angle = (angle + math.pi) % (2 * math.pi) - math.pi

You could try using :Angle. You might need to make the angle negative.

Code:

local GoalLookVector = (Target.Value:GetPivot().Position - SwatPivot.Position).Unit
local CurrentLookVector = SwatPivot.LookVector

local angle = CurrentLookVector:Angle(GoalLookVector)
Rotate.AngularVelocity = Vector3.new(0, angle, 0)

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