Trouble rotating a part to look at another part

Ive been able to accomplish this in the past, but for some reason the code seems to make the part i want to rotate…not rotate properly

here is my code


-----------------
--Head Follow
coroutine.wrap(function()
	while true do
		task.wait(1)
		if IR.Value == false then
			local mag = (PPart.Position - splatModel.Splat_Head.Position).Magnitude
			if mag <= 50 then
				print("ermm")
				TS:Create(neck6D, TweenInfo.new(1),{C1 = CFrame.new(neck6D.C1.Position, PPart.Position)}):Play()
			else
				neck6D.C1 = neckC1
			end
		else

		end
	end
end)()

I even tried CFrame.LookAt but the same result came out. Im not sure what the issue could be

Help is greatly apriciated ^^

thank you for your time :slightly_smiling_face:

  • You should use CFrame.lookAt, CFrame.new(Vector3, Vector3) is deprecated.
  • It needs to be in object space. To do this, you can add :ToObjectSpace(rootPart.CFrame)
1 Like

You can just do

local part1 = workspace.Part1
local part2 = workspace.Part2
local pos = CFrame.new(part1.Position, part2.Position)
part1.CFrame = pos
1 Like

could you demonstrate how i can use it?

C1 = CFrame.lookAt(neck6D.Part0.Position, PPart.Position):ToObjectSpace(neck6D.Part0.CFrame)
you might need to :Inverse() it.

Also, I strongly recommend using Weld instead of Motor6D for this.

1 Like

It kind of worked…kind of

the robot model is rigged for animation, i cant use welds for it im afriad

heres the new code line

TS:Create(neck6D, TweenInfo.new(1),{C1 = CFrame.lookAt(neck6D.Part1.Position, PPart.Position):ToObjectSpace(neck6D.Part0.CFrame)}):Play()

Okay. So, for the orientation issue, you want to add .Rotation at the end of the target CFrame and multiply it by CFrame.Angle(0,0,math.pi/2) (might be different depending on how it’s rigged).

1 Like

so it would be
CFrame.LookAt(pos, target.Orientation)?

No. The tween code would be:

TS:Create(neck6D, TweenInfo.new(1),{C1 = CFrame.lookAt(neck6D.Part0.Position, PPart.Position):ToObjectSpace(neck6D.Part0.CFrame).Rotation*CFrame.Angles(0,0,math.pi/2)}):Play()

also changed Part1 to Part0 to prevent the head from going up slightly every time it tweens

1 Like

Would i have to use math.rad for the angles part? or isthat depricated aswell?

No, I think you’re confused. CFrame.Rotation is not the same as CFrame:ToOrientation, where CFrame:ToOrientation returns three rotational values.

math.rad is not deprecated and it converts degrees to radians.

1 Like

@SubtotalAnt8185

So the floating part is fixed, but the heads orientation is still an issue. My first thought was to switch “math.pi” to a different axis, but im not so sure :sweat_smile:

Yes, that’s likely what you need to do. math.pi is 180 degrees, dividing it by 2 is 90 degrees.

1 Like

it worked slightly, but the head is still rotating weirdly. I think its because the models head is a union, so i’d have to do further testing

Thank you so much for your help!

Sometimes, you might need to rotate it on more than one axis. Try that.

1 Like