# How to Clamp velocity?

Attention Topic was automatically imported from the old Question2Answer platform.

I have a Character that Jumps, if you jump into a Cylinder or the Edge of a Box Collider, Instead of Having Velocity Hault (Although the Character Stops from Collision), You will keep your Velocity and Continue to fly forward once you no longer hit with the Collider

How do you Break Down Velocity according to the Direction of Movement?
(I have the Current Velocity Vector Saved Properly through the CharacterBody3D)

I also have another Vector3 Variable (MoveDirection), Which is the Main cause for the CharacterBody3D’s Change in Velocity…

MoveDirection is based on Movement Inputs with, Camera.rotated()

I would greatly appreciate any help that could push me towards understanding this, Thank you

1 Like

Both `Vector2` and `Vector3` have a `clamp()` method. I assume that’s what you’re looking for?

I have been able to clamp my “MoveDirection”… for Instance, If I have:

`clamp(MoveDirection.x, -.15, .15)`

The Direction Will Successfully Clamp on the X axis, But not according to the Character, But Globally, Which is good

But I need to have a Clamping Variable, Which will Clamp this Vector3 according to where a Velocity is quickly Haulted (The Direction of the Hit Surface)

I can use CharacterBody3D’s velocity to determine where to Add the Clamp based on its immediate Change? Therefor I am trying to figure out how I can achieve this

Nickyroo | 2023-06-15 16:57

Ah, got it. You probably want something like:

``````var v3 = Vector3(10,10,10)
v3 = v3.normalized() * 2
``````

That’ll create a new vector with length 2 from the original vector (keeping the same direction). So, just normalize your original vector and multiply it by the clamped length you want…

jgodfrey | 2023-06-15 17:03

I am a bit Confused how to use this formula, here is what I am currently using if it could help…

``````if Vector3(MainBody.velocity.x, 0, MainBody.velocity.z) != (MoveDirection * BaseSpeed):
print("HittingWall")
``````

(the y-axis is not being Used for MoveDirection, that Axis is seperate with its own features)

I have Tried to Clamp MoveDirection (Clamping the Velocity of MainBody does nothing), But I keep having issues where I begin to Bounce off into space or the Screen Crashes when Interacting from Certain Angles

Do you know how to properly Clamp a Vector3 Velocity? (This would apply to MoveDirection since (MoveDirection * 2) == the Velocity when not stopping on a surface)

Also the Console does print “HittingWall” whenever I push into a surface, And if I release the Keys it successfully Haults printing

Nickyroo | 2023-06-15 17:36

I am testing this var that you have shown me, I believe this is the Correct Answer given I make Correct Configurations, But I need to of course, further progress with Construction of the Clamp involving this formula… For instance I notice in the Debugging, “v3” Correctly Pushes toward the Walls angle when Hitting, Working on all Sides which is great! and I can see that this is a correct piece of the puzzle

But I will not be able to further test or respond for at least 6 hours due to my Schedule… But Thank you, your Help has been much appreciated! and If this is Successful I will label my Question as Answered by you

Nickyroo | 2023-06-15 17:52

Do you know how to properly Clamp a Vector3 Velocity?

The above code (based on `normalize`) is the correct way to change the length of a vector without changing its direction - assuming you know the length you want the vector to be.

If you still want to use the in-built `Vector3.clamp()` method to limit the length of your vector based on a min and max vector, you could do something like this:

``````var v_org = Vector3(10,10,10)
var v_min = v_org.normalized() * 1
var v_max = v_org.normalized() * 5
var v_new = v_org.clamp(v_min, v_max)
print("Org Vector: %s, %s" % [v_org, v_org.length()])
print("New Vector: %s, %s" % [v_new, v_new.length()])
``````

prints:

``````Org Vector: (10, 10, 10), 17.3205089569092
New Vector: (2.886751, 2.886751, 2.886751), 4.99999952316284
``````
• v_org = the original vector needing to be clamped
• v_min = a vector, based on the original, created with the minimum length (1 here)
• v_max = a vector, based on the original, created with the maximum length (5 here)

Notice that the orignal vector has a length of ~17.3. I’ve elected to clamp that vector’s length to be between 1 and 5 (the length of min and max vectors). The resulting vector still has the same direction as the original, but has a length of 5.

jgodfrey | 2023-06-15 18:11

I do not understand what %s does

Nickyroo | 2023-06-16 04:21

It’s not important for what I was trying to show but it’s just a placeholder in the string where variable values will be inserted.

jgodfrey | 2023-06-16 11:02