Motor6D rotation problems when facing mouse direction

I’ve been trying for a while to get this to work but with no luck.
What I want is for the player’s head and arm joints to rotate to face the mouse direction, while still leaving the rig itself fully animatable.

I’ve tried IK Controllers and multiple coded solutions, the one I have now being the closest I’ve gotten to a working version.
It positions the joints correctly and rotates them based on mouse direction, but the rotation is backwards for some reason.
If you look left, the joints go right and if you look up, they go down, etc.

I’ve tried using Inverse() in various parts of the code and simply reversing each part of the rotation matrix separately, but any modification at all just seems to mess up the rotation completely.

There’s a lot of other posts on making limbs face the mouse, but all either replace the joints or override them in such a way that they can’t play animations normally.

This is meant to be used with an R6 character:

local RS = game:GetService("RunService").RenderStepped
local PL = game:GetService('Players').LocalPlayer
local Mouse = PL:GetMouse()

local Char:Model = script.Parent

local Neck = Char:WaitForChild('Torso'):WaitForChild("Neck")
local RightShoulder = Char.Torso:WaitForChild("Right Shoulder")
local LeftShoulder = Char.Torso:WaitForChild("Left Shoulder")

local JointC0s = {}


local function C0_To_Point(Joint:Motor6D, PointDir:Vector3):CFrame
	if not JointC0s[Joint.Name] then
		JointC0s[Joint.Name] = Joint.C0
	end
	
	local Joint_C0 = JointC0s[Joint.Name]
	local BaseCF = Joint.Part0.CFrame * Joint_C0
	
	PointDir = CFrame.lookAt(BaseCF.Position, BaseCF.Position + PointDir):ToObjectSpace(BaseCF)
	
	return CFrame.new(Joint_C0.Position) * PointDir.Rotation
end


RS:Connect(function()
	local PointDir = Mouse.Origin.LookVector
	
	Neck.C0 = C0_To_Point(Neck, PointDir)
	RightShoulder.C0 = C0_To_Point(RightShoulder, PointDir)
	LeftShoulder.C0 = C0_To_Point(LeftShoulder, PointDir)
end)

Any help is very appreciated as I generally am out of ideas.
Thanks.

Why wont you try to :Inverse() or use C1 instead?

Ideally you should be using .Transform property instead if possible.

As I mentioned in the post, I already tried :Inverse() but it only messed up the rotation further.
I’m not using C1 because I want to rotate at the joint position, not the actual limb position which is what C1 would get me.

I don’t want to use Transform because it’d mess with animation playback which also modifies that property.

I figured it out myself.
The solution was to multiply the inversed Torso CFrame by “PointDir” instead of C0.

I actually used Studio assistant of all things to help.
The actual code it gave me was wrong of course, but it gave me some ideas for new things to try.

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