C0 animation works in one direction but not others

I have the following in a local script.

local char = game.Players.LocalPlayer.Character
local root = game.Players.LocalPlayer.Character.HumanoidRootPart
local aimMotor = char.Torso:FindFirstChild("Right Shoulder"):Clone()
local baseC0 = char.Torso["Right Shoulder"].C0
aimMotor.Name = "Aim Motor"
aimMotor.Parent = char.Torso



game:GetService("RunService").Stepped:Connect(function()
	
	local mousePos = game.Players.LocalPlayer:GetMouse().Hit.Position
	
	-- Aim mouse
	local offset = CFrame.lookAt((root.CFrame * baseC0).Position, mousePos)
	local aimCFrame = baseC0:ToObjectSpace(offset) * CFrame.Angles(0, math.rad(90), math.rad(90))
	aimMotor.Transform = aimCFrame.Rotation
	
	
end)

It attempts to aim the player’s arm at the cursor. This works fine when I face one direction, but not the rest of them.


It only seems to care about the orientation of the player’s body. I’m assuming it’s that I did something relative to something where I should have done it relative to something else, but I have no idea where.
What went wrong? How do I make it work?

2 Likes

This is because, in R15 rigs, the baseC0 value is fixed in the local coordinate space of Torso or UpperTorso. You are using a world position mousePos when you compute the aiming offset CFrame.lookAt, but the transformation does not take into account the player’s current rotation properly in all directions.

1 Like
  1. I’m using R6, does that change anything? You said torso or uppertorso, which seems different for each rig, but you said in R15 rigs at first, so I wanted to double check
  2. How would I actually fix this? If I understand correctly, you’re saying I have to make the mousePos relative to the player’s rotation, but how exactly do I do that? Or should I be fixing it a different way?
1 Like
local player = game.Players.LocalPlayer

local char = player.Character or player.CharacterAdded:Wait()
local root = char:WaitForChild("HumanoidRootPart")

local aimMotor = char.Torso:FindFirstChild("Right Shoulder"):Clone()
local baseC0 = char.Torso["Right Shoulder"].C0
aimMotor.Name = "Aim Motor"
aimMotor.Parent = char.Torso

game:GetService("RunService").Stepped:Connect(function()
	local mousePos = player:GetMouse().Hit.Position

	local mouseRelativePos = root.CFrame:pointToObjectSpace(mousePos)

	local offset = CFrame.lookAt(Vector3.new(0, 0, 0), mouseRelativePos)
	local aimCFrame = baseC0:ToObjectSpace(offset) * CFrame.Angles(0, math.rad(90), math.rad(90))
	aimMotor.Transform = aimCFrame.Rotation
end)

I tried this and it seemed to work

2 Likes

The changes I noticed were here:

I’m assuming they’re the only ones
the pointToObjectSpace() makes sense; I don’t know what it does, but the name seems obvious and I’ll look it up

I am curious though, why do we need to change the vector position to be 0, 0, 0 for the lookAt?

1 Like

Vector3.new(0, 0, 0) is just the “starting” point of the rotation. You’re only interested in the direction from the player’s body to the mouse, so we can think of the player’s body as rotating from the origin (0,0,0) toward the mouse position.

1 Like

I’ve switched this to work with the server by changing the .Transform statement to

aimMotor.C0 = baseC0 * aimCFrame.Rotation

which seems to work fine.
The problem is that this doesn’t really work when I add in a second component, an animation that I want to play with it. The animation is simple: all it does is turn your player to the side, and gives a blank keyframe to overwrite player animations.
However, when I run it…

I tried instead changing some of the angles offset, from

local aimCFrame = baseC0:ToObjectSpace(offset) * CFrame.Angles(0, math.rad(90), math.rad(90))

to

local aimCFrame = baseC0:ToObjectSpace(offset) * CFrame.Angles(0, 0, math.rad(90))

Which looks like it works fine for horizontal movement, but then…

I wanted to aim the player’s body at the cursor anyways, so I just adjusted that script, which works, to be offset slightly, giving this:


It works, but the animations are a little choppy, and, most importantly, the animations, like walking and idle, are still affecting the CFrame!
So, either

  • How do I fix the problem with animations, or
  • How do I fix the choppiness and/or at least make it immune to the player’s animations?
1 Like

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