Part Rotation in Direction of Player

I have a part that moves with the character, and I’m attempting it to make it rotate in the direction that the player is walking. However, the issue can be seen in the video below:

And here is the code, which is binded to the render step:

local character = player.Character
local moveDirection = character.Humanoid.MoveDirection
local lookVector = character.HumanoidRootPart.CFrame.LookVector
local part = character.Part
local radius = part.Size.X * 0.5

-- Position the part at an offset from the player
local offsetVector = Vector3.new(lookVector.Unit.X, radius - 3, lookVector.Unit.Z)
part.Position = character.HumanoidRootPart.Position + lookVector.Unit * radius + offsetVector

-- Rotate the part in the direction the player is facing
local rotationAxis = lookVector:Cross(-Vector3.yAxis).Unit
part.CFrame *= CFrame.fromAxisAngle(rotationAxis, moveDirection.Magnitude/10)

The rotationAxis is the orthogonal vector axis that the part should be rotating about, and I have confirmed this vector is correct and that the part, when testing, isn’t actually rotating about this vector axis.

I also tried re-orienting the part prior to rotating:

part.CFrame = CFrame.lookAt(part.Position, part.Position + lookVector)

But this seems to stop the rotation entirely.

So, is this an issue with how I’m applying the rotation transformation or is there something else I am missing?

1 Like

Update:

I was overthinking the whole vector approach, and the solution is to just change the part’s orientation directly:

-- Set Y orientation to that of the HRP
part.Orientation = Vector3.new(part.Orientation.X, character.HumanoidRootPart.Orientation.Y, 0)

-- Decrease X orientation for the rotating animation
part.Orientation -= Vector3.new(moveDirection.Magnitude, 0, 0)

-- Reset once it hits minimum, -90
if part.Orientation.X <= -89 then
	part.Orientation = Vector3.new(0, part.Orientation.Y, 0)
end

Hopefully this helps anyone who has a similar issue in the future.

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