Help with AlignOrientation

I’m working on a Humanoid-less entity. So far, I’ve rigged it and made it move. It’s oriented with AlignOrientation’s. The issue I have, is rotating the entity so it faces in the direction it’s going with code. If you call MoveTo() on a humanoid, the character looks where it’s going.

I have no idea how I can do this (I know how to orient it), but how would I get the orientation of where it is going?

This is my current code: (Note ThePart is just the destination part, and “Fig” is the Torso.)

	local Direction = (ThePart.CFrame.p - Fig.CFrame.p).Unit * 18

	Fig.BodyPosition.Position = ThePart.Position
	Fig.BodyVelocity.Velocity = Direction
	
	
	for _, PartToRotate in ipairs(Fig.Parent:GetChildren()) do
		if PartToRotate:IsA('BasePart') and PartToRotate:FindFirstChild('AlignAttach') then
			PartToRotate.AlignAttach.Orientation = ThePart.Orientation
		end
	end

It works fine if the destination part is oriented correctly, but PathfindingService doesn’t do this. How would I always orient the character in the direction that it is going?

Use CFrame.lookAt towards the move direction.

My version of non humanoid movement for rotation only:

1 Like

That’s useful, but how would I get the orientation (just the Y axis on the orientation), from those 3 numbers? I just need to set the attachments orientation, but I need to get that orientation from CFrame.lookAt

Attachments already have a CFrame property or a WorldCFrame property, choose whichever one works.

Otherwise you can use :ToOrientation().

I gave it a go using this code:

local function UpdateRotation(Humanoid, Attachment, Dest)
	if Humanoid.Velocity.Magnitude > 0 then
		local testsmth = CFrame.lookAt(Vector3.zero,Humanoid.Velocity):ToEulerAngles()
		print(testsmth)
		Attachment.Orientation = Vector3.new(0,testsmth,0)
	else
		--Maintain current orientation
		--local _, y, _ = Humanoid.Parent.Parent.Torso.CFrame.Rotation:ToOrientation()
		--Attachment.CFrame = CFrame.fromOrientation(0, y, 0)
	end
end

Note the humanoid is actually just the BodyVelocity!!

It seems to work fine on the small (like very small turns), but anything sharp and it doesn’t seem to turn at all.
Here is the sharp turn

It does seem to work on these small turns though

The numbers it’s printing seem to be way to small, I mean, for the sharp turn shouldn’t it be printing something like 90? Yet it’s printing about 1.

1 Like

Read the documentation, although it seems the new one isn’t that clear:

This matrix is used internally when doing calculations involving rotations. They use radians as their unit (for conversion to degrees, use math.rad() or math.deg()).

So are you saying I’d need to use math.deg?

Even using math.deg, it still doesn’t seem to orient toward the part at all…

Apologies these were just suggestions and not always the full path towards your 100% solution.

I can also suggest looking at your align orientation settings is it at one attachment mode or two attachment mode?

If it is in two attachment mode I suggest placing the attachment0 at the rootpart (standard) and attachment1 at terrain.

Not too sure how it was setup in this problem.

This was how I done it for mine in 2 attachment mode:

	local rootVelocity = rootPart.AssemblyLinearVelocity
	local xzVelocity = rootVelocity*XZ_VECTOR
	local xzSpeed = xzVelocity.Magnitude

	--HumanoidAutoRotate
	local unitXZ = (xzVelocity).Unit
--Magnitude check and == is to avoid NAN errors
	if moveDirection.Magnitude > 0 and unitXZ.X == unitXZ.X then
		alignmentAttachment.CFrame = CFrame.lookAt(ZERO_VECTOR, moveDirection)
	else
		local x, y, z = humanoidRootPart.CFrame.Rotation:ToOrientation()
		alignmentAttachment.CFrame = CFrame.fromOrientation(0, y, 0)
	end

	visualizePart.CFrame = alignmentAttachment.CFrame.Rotation+humanoidRootPart.Position+Vector3.new(0,10,0)

Adding a visualization part is also pretty helpful as well, this is the old v1 physics version with no animations link here for reference, probably much easier to understand:

Alright, so, now it turns. The only thing is it turns opposite to the way it’s facing, so it’s now moving backward.

This was my code:


local function UpdateRotation(Humanoid, Attachment, Dest, alpha)
	if Humanoid.Velocity.Magnitude > 0 then
		--local testsmth = CFrame.lookAt(Vector3.zero,Dest.Position):ToOrientation()
		--print(testsmth)
		--Attachment.Orientation = Vector3.new(0,math.deg(testsmth),0)

		--alpha = alpha or 1
		--local lookAt = CFrame.lookAt(Humanoid.Parent.Parent.Root.CFrame.p,Dest.Position)
		--local cframe = Humanoid.Parent.Parent.Root.CFrame
		local XZ_VECTOR = Vector3.new(1,0,1)
		--local final = CFrame.fromMatrix(cframe.p,lookAt.XVector,cframe.YVector):ToEulerAngles()

		local rootVelocity = Humanoid.Parent.Parent.Root.AssemblyLinearVelocity
		local xzVelocity = rootVelocity*XZ_VECTOR
		local xzSpeed = xzVelocity.Magnitude


		--HumanoidAutoRotate
		local unitXZ = (xzVelocity).Unit
		--Magnitude check and == is to avoid NAN errors
		if Humanoid.Velocity.Magnitude > 0 and unitXZ.X == unitXZ.X then
			local brahh = CFrame.lookAt(Vector3.zero, -Humanoid.Velocity)
			local x, y, z = brahh:ToOrientation()
			Attachment.CFrame = CFrame.fromOrientation(0, y, 0)
		else
			local x, y, z = Dest.CFrame.Rotation:ToOrientation()
			Attachment.CFrame = CFrame.fromOrientation(0, -y, 0)

			--Attachment.Orientation = Vector3.new(0,math.deg(-final),0)
		--Maintain current orientation
		--local _, y, _ = Humanoid.Parent.Parent.Torso.CFrame.Rotation:ToOrientation()
			--Attachment.CFrame = CFrame.fromOrientation(0, y, 0)
		end
		
		--visualizePart.CFrame = alignmentAttachment.CFrame.Rotation+humanoidRootPart.Position+Vector3.new(0,10,0)
	end
end

Any idea on how to fix this?