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??
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
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]
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 :^)