NPC Keeps Rotating

Hi all,

I managed to make non-humanoid NPCs in order for me to be able to place hundreds of them without hurting the game’s performance & everything is handled locally.

I have placed a bunch of nodes (walk points) around the sidewalk and I’m using TweenService to move the characters:

Now, the problem I’m having is that the characters rotation is always set to the node’s orientation. That wouldn’t be a problem if it wasn’t for the fact that the character is going back once he has finished the path, essentially making round-trips in a loop.

Local Script:

for _, node in pairs(array) do
	NPC.HumanoidRootPart.CFrame = CFrame.lookAt(NPC.HumanoidRootPart.Position, node.Position) 
	local goal = {}
	goal.CFrame = CFrame.new(node.Position)
	local tweenInfo = TweenInfo.new(2.5, Enum.EasingStyle.Linear)
	local tween = TweenService:Create(NPC.HumanoidRootPart, tweenInfo, goal)
	tween:Play()
	tween.Completed:Wait()
end

What should I add to goal.CFrame to make sure the orientation is set to be the NPC pointing to the node instead of taking the node’s orientation?

Thanks in advance!

1 Like

you should actually add a position to the CFrame’s 2nd parameter, the position to look at.

for _, node in pairs(array) do
	NPC.HumanoidRootPart.CFrame = CFrame.lookAt(NPC.HumanoidRootPart.Position, node.Position) 
	local lookAt = Vector3.new(node.Position.X, NPC.HumanoidRootPart.CFrame.Position.Y, node.Position.Z)
	local goal = {}
	goal.CFrame = CFrame.new(node.Position, lookAt)
	local tweenInfo = TweenInfo.new(2.5, Enum.EasingStyle.Linear)
	local tween = TweenService:Create(NPC.HumanoidRootPart, tweenInfo, goal)
	tween:Play()
	tween.Completed:Wait()
end

The NPC is going absolutely crazy now

Try and change the orientation of the goal maybe?

for _, node in pairs(array) do
	local lookAt = Vector3.new(node.Position.X, NPC.HumanoidRootPart.CFrame.Position.Y, node.Position.Z)
	NPC.HumanoidRootPart.CFrame = CFrame.lookAt(NPC.HumanoidRootPart.Position, node.Position)
	goal.CFrame = CFrame.new(node.Position)
	goal.Orientation = lookAt:ToOrientation()
	local tweenInfo = TweenInfo.new(2.5, Enum.EasingStyle.Linear)
	local tween = TweenService:Create(NPC.HumanoidRootPart, tweenInfo, goal)
	tween:Play()
	tween.Completed:Wait()
end

You can’t use :ToOrientation() on Vector3, it only works with CFrames

In your original script, change this to:

goal.CFrame = NPC.HumanoidRootPart.CFrame:ToOrientation() + node.Position
and give it a try.

:ToOrientation() actually returns 3 numbers instead of a Vector3 or a CFrame value. To get these values, you should do:

x,y,z = CFrame:ToOrientation()

image

And because of that, you can’t exactly add a number to a Vector3

1 Like

is there any invisible wall or something ._.

No, why should that make a difference anyway? NPCs are non-humanoids with all their parts uncollidable and untouchable.

Not sure but, could it be because AutoRotate is enabled?

These are non-humanoids.

character limitttttttttttt

Ah, didn’t read the previous replies sorry.

EDIT: Didn’t realize it was in your topic! Oops.

Ah, my fault. Here:

for _, node in pairs(array) do
	local lookAt = Vector3.new(node.Position.X, NPC.HumanoidRootPart.CFrame.Position.Y, node.Position.Z)
	NPC.HumanoidRootPart.CFrame = CFrame.lookAt(NPC.HumanoidRootPart.Position, node.Position)
	goal.CFrame = CFrame.new(node.Position)
	goal.Orientation = CFrame.new(node.Position, lookAt):ToOrientation()
	local tweenInfo = TweenInfo.new(2.5, Enum.EasingStyle.Linear)
	local tween = TweenService:Create(NPC.HumanoidRootPart, tweenInfo, goal)
	tween:Play()
	tween.Completed:Wait()
end

The NPC is flying around and flinging once again.

Can you see if the HumanoidRootPart of your NPC is rotating or if it’s looking at the node? Just to deduce if it’s a problem with changing its CFrame or the NPC as a whole.

Ah, maybe it was CFrame.Rotation that I meant to put. The new code should actually be goal.CFrame = NPC.HumanoidRootPart.CFrame.Rotation + node.Position.

1 Like

Thank you, this is the solution. Everything works perfectly fine now.

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