Humanoid.MoveTo with CFrame.lookAt

I have a npc that i can control by clicking and it moves to that position. I add a “Lock On” system which essentially just toggles it to lock onto whatever the closest target is. Example:

						myDoll:SetPrimaryPartCFrame(CFrame.lookAt(
							myDoll.HumanoidRootPart.Position,
							closest.Position * Vector3.new(1,0,1)
								+ myDoll.HumanoidRootPart.Position * Vector3.new(0,1,0)
							))

This works however when paired with Humanoid.MoveTo(Position) this causes the model to never move to the node position and stuck frozen staring at the closest target. How would i achieve Humanoid.MoveTo() whilst making the model maintain its orientation of the closest target.

1 Like

Since Humanoid:MoveTo() depends on Vector3 positions, why not try to use the Position property from the CFrame made from CFrame.lookAt()

How exactly would that work? I’m not too familiar with lookAt() How exactly would you go about tweening position whilst keep relativity to world space via for example if theirs a ramp its going to walk up normally instead of just calculating half the size multiply itself + model half size

You don’t have to do all that. Humanoid:MoveTo() makes it so that the humanoid walks in a straight path to the target. However, I would prefer you using PathfindingService.

??? even if i use pfs compute to generate waypoints that doesn’t answer the question, that just makes an attempt at trying to generalize my current code as something stereotypical, Applying the CFrame.p is only going to make the char walk in that direction… I’m making a lock-on System, for example:
image
Blue = Doll
Red = Waypoint
Yellow = Intended Orientation
Green = The locked on target

What your behaviour would achieve:
image

Your resolution doesn’t take into account that I need to maintain a orientation whilst moving to a different “Targets” position. Point A Dictates position, Point B Dictates orientation. I already achieved both behaviours, now I need them to both work without issue. The problem with tweening position is that it wont “walk”.

I don’t get it. Are you trying to make it so that the doll moved to a locked on target whilst keeping the orientation staring at them?

Ok so imagine theirs a Finish line, Now the player wants to reach that finish line so he Humanoid.WalkTo to the finish line, Now as he is running he noticed someone in the crowd and is running to the finish line whilst maintaining an orientation facing the direction of the person in the crowd

function CFrameToOrientation(cf)
	local x, y, z = cf:ToOrientation()
	return Vector3.new(math.deg(x), math.deg(y), math.deg(z))
end

i’ve now applied this and the model has finally stopped spinning and flying around. It maintains facing the correct direction however now it never walks towards the position given when doing Humanoid.MoveTo() Instead it starts floating and moving towards the same corner every single time regardless of the nodes position. Why is Humanoid.MoveTo causing this behaviour when i’m not even clashing with the position all I’m doing is changing orientation.

This behaviour I’ve wanted has now been achieved.
To achieve this several things had to be done, firstly I needed a BodyGyro and to have AutoRotate on humanoid turnt off when locking onto a target so it doesn’t glitch around as its two colliding orientations.
When lock-on is turnt off it will set the bg cframe to whatever the current target (or nil if thats possible as im still messing around with behaviour)

						local direction = ((closest.CFrame).p - Target.HumanoidRootPart.Position) * Vector3.new(1, 0, 1)
						bg.cframe = CFrame.new(Target.PrimaryPart.Position, Target.PrimaryPart.Position + direction)

closest = the Locked on Target
Target = The model locking onto the target

2 Likes

Sorry for the late reply and unable to help you, I was wondering are you using the Unit property to find the direction?

No, How I find direction is as shown in the codeblock above.

Hey im trying to figure out a solution to an exactly similar problem. Since there’s no more BodyGyro, I’m not sure what to do. Im also dealing with the stuck character issue:

-- Run Service
local runService = game:GetService('RunService')

-- Characters
local p1 = workspace:WaitForChild('p1')
p1:FindFirstChild('Humanoid').AutoRotate = false
local p2 = workspace:WaitForChild('dishwasher_salesman')
p2:FindFirstChild('Humanoid').AutoRotate = false

task.wait(3)
runService.Heartbeat:Connect(function()
	local p1RootPart = p1:FindFirstChild('HumanoidRootPart')
	local p2RootPart = p2:FindFirstChild('HumanoidRootPart')
	
	p2RootPart.CFrame = CFrame.lookAt(p2RootPart.Position, Vector3.new(p1RootPart.Position.X, 3.196, p1RootPart.Position.Z))
end)

BodyGyro still exists, its just no deprecated meaning it wont receive future updates, if you still don’t wish to use BG, then find its alternative which is part of the new package of instance forces to replace Bodymovers

AlignOrientation is the closest thing I can find however, it requires attachments. Thanks