Cleaning up NPC and player rotation

This just gets both the player and an NPC to rotate so they face each other, but I wanna clean it up a little. Is there a way I can make this lil tidier??

local Goal = {
		CFrame = CFrame.new(
			NPC.Current.HumanoidRootPart.Position,
			Vector3.new(
				Character.HumanoidRootPart.Position.X,
				NPC.Current.HumanoidRootPart.Position.Y,
				Character.HumanoidRootPart.Position.Z
			)
		)
	}
	
	local Tween = TweenService:Create(NPC.Current.HumanoidRootPart, TweenInfo.new(), Goal)
	Tween:Play()
	
	local Goal = {
		CFrame = CFrame.new(
			Character.HumanoidRootPart.Position,
			Vector3.new(
				NPC.Current.HumanoidRootPart.Position.X,
				Character.HumanoidRootPart.Position.Y,
				NPC.Current.HumanoidRootPart.Position.Z
			)
		)
	}
	
	local Tween = TweenService:Create(Character.HumanoidRootPart, TweenInfo.new(), Goal)
	Tween:Play()

If it’s unanchored, use body gyros

1 Like

Try more than just TweenInfo.new() Here’s how it works:

local Info = TweenInfo.new(2, Enum.EasingStyle.Linear,Enum.EasingDirection.In, 0, false, 0) 

Here’s it explained for you

2 would be the time it takes to reach the goal
Enum.EasingStyle.Linear is, well, the EasingStyle
Enum.EasingDirection.In is the EasingDirection
0 is how many times it will repeat
false is if it will reverse when it is completed
and 0 is the Delay between reverses or when it starts

Try that, and see if it looks better

Here’s it put inside of the script:

local Goal = {
CFrame = CFrame.new(
NPC.Current.HumanoidRootPart.Position,
Vector3.new(
Character.HumanoidRootPart.Position.X,
NPC.Current.HumanoidRootPart.Position.Y,
Character.HumanoidRootPart.Position.Z
)
)
}

local Tween = TweenService:Create(NPC.Current.HumanoidRootPart, TweenInfo.new(2, Enum.EasingStyle.Linear,Enum.EasingDirection.In, 0, false, 0) , Goal)
Tween:Play()

local Goal = {
	CFrame = CFrame.new(
		Character.HumanoidRootPart.Position,
		Vector3.new(
			NPC.Current.HumanoidRootPart.Position.X,
			Character.HumanoidRootPart.Position.Y,
			NPC.Current.HumanoidRootPart.Position.Z
		)
	)
}

Agreed, or AlignOrientation

They’ll handle the rotations using the physics engine so the Humanoid can still move around!

1 Like

I just use CFrame for this and change every Heartbeat:

game:GetService(“RunService”).Heartbeat:connect(function()
   npc:SetPrimaryPartCFrame(npc:GetPrimaryPartCFrame().p, plr:GetPrimaryPartCFrame().p)
end)

You could use TweenService but this way is way cleaner imo.

I’m gonna say something that is a matter of style. There is no good reason to not invent a lot of extra variables (as long as they go out of scope soon). There are two good reasons to do so.

One, if you access NPC.Current.HumanoidRootPart.Position then you have to index all those objects 1 by one. After doing that the first time, store the reference in a variable. There is no real issue about memory here.

Second, the most important reason is readability. I don’t want to see all these long lines and characters. Extra variables with well-chosen names help me understand what goes on on a higher level.

local charRp = Character:WaitForChild("HumanoidRootPart")
local charRpPos = charRp.Position
local npcRp = NPC:WaitForChild("Current"):WaitForChild("HumanoidRootPart")
local npcRpPos = npcRp.Position
local directionTarget = Vector3.new(charRpPos.X, npcRpPos.Y, charRpPos.Z)
local Goal = {
		CFrame = CFrame.new(npcRpPos, directionTarget)
	}

Edit: whoops pressed publish too soon. Not quite perfect now but you get the idea.

It’s much more easier and smoother really to use a Body Mover to achieve having the NPC rotate towards the character, but after all it depends on what you want to do with it.

I have my own NPC system and this code will cause only the player to rotate in the direction of the NPC [Figure 1]. Instead of making the NPC rotate, I just have the NPC look at the player when in close proximity to add more detail. [Figure 2]

    MyHumanoid.AutoRotate = false
    
    local bodyGyro = Instance.new("BodyGyro", MyCharacter.PrimaryPart)
	bodyGyro.Name = "FaceDialoguePoint"
	bodyGyro.D = 100
	bodyGyro.P = 1000
    bodyGyro.MaxTorque = Vector3.new(0, 1000, 0)
    bodyGyro.CFrame = CFrame.new(Vector3.new(), NPC.PrimaryPart.RootRigAttachment.WorldPosition - RootPart.Position)

FIGURE 1

FIGURE 2

My code that I provided can be taken and easily modified to have both the NPC and player rotate. If you need assistance replicating the code to work with both the NPC and player, just ask :^)

4 Likes