How to get the angle between rightvector and lookvector?

So I have a body velocity script that makes the player move depending on what key the player presses. I want it so that if the player presses w and d, then it gets the angle between. How would I do this? I tried:

bodyvel.Velocity = Vector3.new( (char.HumanoidRootPart.CFrame.LookVector.X + (- 
char.HumanoidRootPart.CFrame.RightVector.X))/2, bodyvel.Velocity.Y, 
(char.HumanoidRootPart.CFrame.LookVector.Z + (- 
char.HumanoidRootPart.CFrame.RightVector.Z))/2 )

But that didn’t work.

The angle between a CFrame’s rightvector and lookvector is always 90 degrees, so you can do:

(char.HumanoidRootPart.CFrame * CFrame.Angles(0, math.rad(45), 0)).Unit
2 Likes

Whats best in this situation is to add the two vectors together and get the unit of that. Then, set the velocity to the unit vector. Since it’s a unit vector now, the base velocity will be the same amount in any direction.

This accounts for pressing opposite keys at the same time. Adding all the vectors, in this case, opposite vectors, will cancel each other out, leaving the character motionless if you press W and S at the same time.

Or, if you have W, S, and A pressed together, you will be moving left, because W and S will cancel out, but A will be left alone.

1 Like

I kinda understand you. Is there any way you could explain it more?

EDIT: This solution is not relevant to the initial question, but instead solves OP’s problem.

So say you have two vectors, 1,0,0 ( true x ), and 0,0,1 ( true z ).

If you add the two together, it becomes 1,0,1 , which is an exact diagonal toward both positive x and z.

If you get the unit of this, instead of being a longer than 1 stud diagonal length, it would become exactly 1 stud long. This means that regardless of the direction you create by adding horizontal vectors, the force in that direction will always be 1 once you make it a unit vector.

Multiply that unit vector by a walk speed value, and you are walking at a constant speed in any direction you want.

In a regular use case, the vectors you add are the camera’s lookVector, rightVector, leftVector, and backVector. Only the horizontal components of each course.

You only add each one if their corresponding directional key is pressed. So if I have W and D pressed, I would add the lookVector and the rightVector together, and exclude everything else. Unit it, and then multiply by our walk speed value. Done.

1 Like

Would a script like this one do:

bodyvel.Velocity = Vector3.new( (char.HumanoidRootPart.CFrame.LookVector.X - 
char.HumanoidRootPart.CFrame.RightVector.X).Unit, bodyvel.Velocity.Y, 
(char.HumanoidRootPart.CFrame.LookVector.Z - 
char.HumanoidRootPart.CFrame.RightVector.Z).Unit)

I believe he meant something like this:

bodyvel.Velocity = (char.HumanoidRootPart.CFrame.LookVector + char.HumanoidRootPart.CFrame.RightVector).Unit
1 Like

Oof, I must be really bad at understanding things. Is there any wiki or post that explains how to get better at this sort of stuff? Thanks for your help.

Sorry, I edited it to make use of the CAMERA’s vectors, not the character / root part.

What I would do is have a final velocity variable that is updated constantly. ( I have used render stepped before. ) It gets the vectors of the camera based on the keys that are currently pressed, and adds them together. Then units them.

FinalVelocity variable should then be constantly fed to the BodyVelocity. Make sure not to include any Y of the final vector.

1 Like

Wiki CFrame page
An extra CFrame page
Wiki Vector3 page

1 Like

I would need the y because the body velocity also moves the player down, as it is part of the game.

Then go for it. For a regular walking system like default Roblox, you don’t need the Y, as jumping can be done easier by applying a single burst of velocity to the root part on the Y axis.

1 Like