Character strafing rotation problem

Yo, so I want to make the player’s RootJoint (R6) rotate in a direction a bit when they strafe left or right. It works until I rotate the camera. It seems that the HumanoidRootPart’s Velocity doesn’t change based off of the direction the player faces.

Here are some demonstration videos:

Intension:

Problem:

Here is my code:

-- Character strafe rotation

local rootJoint = HumanoidRootPart:WaitForChild("RootJoint")
local oldRootJoinC0 = rootJoint.C0

local rotationAmount = 35
local rotationSpeed = 0.1

local function Lerp(a, b, c)
	return a + (b - a) * c
end

RunService:BindToRenderStep("StrafeRotation", Enum.RenderPriority.Character.Value, function()
	local direction = HumanoidRootPart.Velocity
	
	if direction.Z > 2 then
		rootJoint.C0 = rootJoint.C0:Lerp(oldRootJoinC0 * CFrame.Angles(0, 0, math.rad(rotationAmount)), rotationSpeed)
	elseif direction.Z < -2 then
		rootJoint.C0 = rootJoint.C0:Lerp(oldRootJoinC0 * CFrame.Angles(0, 0, math.rad(-rotationAmount)), rotationSpeed)
	else
		if rootJoint.C0 ~= oldRootJoinC0 then
			rootJoint.C0 = rootJoint.C0:Lerp(oldRootJoinC0, rotationSpeed)
		end
	end
end)

You need to look the velocity vector along the humanoid root part’s z direction.
Currently you are just checking if the velocity vector has some velocity along the world z direction, which does not change relative to the hmrp orientation.

Solution:

local direction = HumanoidRootPart.Velocity:Dot(HumanoidRootPart.CFrame.LookVector)
or
local direction = HumanoidRootPart.Velocity:Dot(HumanoidRootPart.CFrame.ZVector)

It may be either one of those depending on how your if statement is set up

This helps fix the issue where the velocity isn’t world based any more. But, I have one more question. I’ve printed the ZVector solution and I’m a little confused on which values would determine moving left or moving right. Moving forward and backwards are simple integers but what about the other directions?

Nvm, Thanks for the help, I just changed the LookVector/ZVector to RightVector, which determines the value for the left and right movements of the player, which LookVector will determine the forward and backwards movements

Solution

local direction = HumanoidRootPart.Velocity:Dot(HumanoidRootPart.CFrame.RightVector)

-- My changed if-statement
if direction > 2 then
	rootJoint.C0 = rootJoint.C0:Lerp(oldRootJoinC0 * CFrame.Angles(0, 0, math.rad(-rotationAmount)), rotationSpeed)
elseif direction < -2 then
	rootJoint.C0 = rootJoint.C0:Lerp(oldRootJoinC0 * CFrame.Angles(0, 0, math.rad(rotationAmount)), rotationSpeed)

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