Help with character tilting (leaning)

I am making a parkour game with a grappling hook (like Spider Man).

I want to make animations procedural, meaning the character leans depending on where it’s moving.
For now I made it lean forward or backward, which was easy enough, because Y vector has only 2 directions.

However I am struggling to make it lean left and right. No matter what I do, left/right leaning works in some directions, but breaks in others (watch the video).

How to make character lean left/right, based on the direction it’s moving?

I watched many other topics with this questions, but all of them have the same exact problem.

2 Likes

Have you tried to transform the Velocity to object space of HumanoidRootPart using VectorToObjectSpace like this?

HumanoidRootPart.CFrame:VectorToObjectSpace(Velocity).X

I have. The problem is, unlike rotation, which is separate for every axis, position (velocity in this case), is interchangable within x and z, depending on what direction you are moving. So I somehow need to get the needed angle to tilt, from both x and z (but ignoring Y), which I don’t know how to do.

Try this
This projects the velocity on the line that’s going in the HumanoidRootPart.CFrame.RightVector direction
And then it turns that velocity into the angle needed to tilt right using cross product

local RightVector = HumanoidRootPart.CFrame.RightVector
local Direction = Velocity

local RightDirection = RightVector * RightVector:Dot(Direction)

local Angle = Vector3.yAxis:Cross(RightDirection)

Watch this video.

Imagine this red part is the Motor6D. I am doing the up and down tilting by rotating it’s X axis (red in video). I get the angle needed to rotate based on it’s Y velocity. This is very easy, because I only need to consider the Y of the velocity, only 1 value.

Now if I want to tilt it left and right, I need to rotate it on the Z axis (blue in video).
The problem with this, is I can’t consider only X of the velocity, or only Z of the velocity. I need to consider them both at the same time, while getting the angle.

I need a 1 number (float) angle, the function you gave is giving a Vector3.
I don’t know how to turn that Vector3 into a number needed for Z tilting.
If you could help on this, it would be much appreciated.

I also tried taking the average (middlepoint) of vector3 X and Z values provided by your function.
Like other methods I used, it tilts correctly in some directions, while wrong in others.

For example when I am moving in the general Z direction, it tilts right when velocity is to the right, and tilts left when velocity is to the left. When moving in the opposite direction, it tilts right when moving to the left, and tilts left when moving to the right.
In the perpendicular axis (X) it doesn’t tilt at all.

Try using this as the Z axis of the rotation

HumanoidRootPart.CFrame:VectorToObjectSpace(Velocity).X

And if it’s not tilting in the correct direction then invert it by adding a - in front of the line

2 Likes

Yep, worked, thanks!

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