Mathf.Clamp for rigidbody2D.velocity.x not working?

Hi everyone!

So I’ve been making a 2D game, using the 2D physics engine and stuff. It’s a platformer. My player character is a little rigidbody person, with a script i made attatched that controls movement and animation. So, what I was trying to get it to do is make it so that when the character is on the ground, the acceleration is almost instant, but when the character is in freefall, there is still some control over horizontal speed, but the acceleration is slow.

Now, I’ve managed to make my script do this fine, but there was a slight problem with this - if the player runs at full speed, then jumps, and then keeps holding the key to run, they would accelerate beyond their normal ground running speed, and this does look a bit rubbish, so what i’m trying to do is clamp the velocity in the x axis.

So I’ve added in this line right at the end of my Function FixedUpdate:

Mathf.Clamp(rigidbody2D.velocity.x, -forwardSpeed, forwardSpeed);

forwardSpeed is a float that represents the maximum speed at which the character runs when grounded.

I was hoping that this would mean that the x velocity would never exceed the normal running speed, but this line appears to have no affect whatsoever.

So, i’ll post some more of the script - i won’t give you all of it - all the animation control and stuff is in there too, i’ll just keep what’s relevant so you don’t waste too much time trawling through all of it trying to find the relevant lines. I can tell you now that the problem is most likely to be in the FixedUpdate function, near the end.

//So, i'll explain what these variables do later on in the script, but i'll just tell you what their values are here.
//It shouldn't matter what their values are, but i might as well tell you anyway.
var forwardSpeed : float;  //5
var airSpeed : float;    //Ok, so this is actually a force. Yeah, i know, it shouldn't be called speed... i was using it for somehting
//different, i havent got around to renaming it yet... this one would be about 10 (the rigidbody's mass is 1, so that would create an
//acceleration of 10ms^-2), but i've set it to like 1000 for testing purposes at the moment
var jumpTime : float;

private var lastCollision : float;
private var grounded : boolean;
private var jumpSpeed : float;
private var collisionInfo : Collision2D;

function Start(){
//So this line here is simply because I find it more convenient to decide on a duration time for a jump rather than a starting speed.
jumpSpeed = jumpTime * -1*Physics2D.gravity.y * 0.5;

function OnCollisionStay2D(collisionInfo){
//This is my way of checking whether the character is grounded or not - basically, if the character's collider is in contact with
//anything, it checks the normal of that contact point or contact points. If it's/they're pointing roughly upwards, the character is
//grounded. The lastCollision bit i'm using because there isn't any functionOnCollisionStayOutOf if you get what I mean. I use that
//variable to set grounded to false if necessary. It'll make more sense later, there's more stuff about it in the fixedUpdate function.
for(var i : int = 0; i < collisionInfo.contacts.length; i++){
if (collisionInfo.contacts*.normal.y>0.9){*

grounded = true;
lastCollision = Time.time;

function FixedUpdate(){
//Ok, this is the bit i was talking about - basically, if we didn’t get grounded set to true this physics step, grounded is set to false.
//This stuff all works so far by the way.
if (Time.time - lastCollision > 0.02){
grounded = false;

//Ok - this should be fairly self explanatory - if we’re grounded, the speed is equal to the… uh… yeah…
if (grounded){
rigidbody2D.velocity.x = Input.GetAxis(“Horizontal”) * forwardSpeed;

//ah - ok, we’re finally here - the horizontal velocity control while in freefall - instead of modifying the velocity directly here, i’m
//using AddForce to give it a gradual acceleration. This does all work still.
else {
rigidbody2D.AddForce(airSpeed*Vector2(Input.GetAxis(“Horizontal”), 0.0));

//Here’s jumping.
if (grounded && Input.GetButton(“Jump”)){
rigidbody2D.velocity.y = jumpSpeed;

//Ok, this is the bit thats not working. As you can see, this is the final line of the FixedUpdate function, so it should happen after
//everything else. The idea behind this line is to clamp the x velocity such that |v|=<forwardSpeed. But this doesn’t have any effect
//at all on what actually happens. I don’ get any error messages or anything, it just doesn’t work.
Mathf.Clamp(rigidbody2D.velocity.x, -forwardSpeed, forwardSpeed);

function Update(){
//oh yeah, i just put this in to make sure i wasn’t imagining stuff. And yes, i was getting completely ridiculous speeds, way way above forwardSpeed.
So yeah, I really have no idea what’s going on here - any help would be very much appreciated. Sorry about the messy code, as you can probably see, it’s a bit longwinded and inefficient, i am quite new to coding and unity. Thanks in advance for any help!
Blessings of the nine divines upon you,

just about how u’ve declared u nearly had it actually all u need to do is tell it what value it should be Clamping, hope this helps u .

// JS
    var f = Mathf.Clamp(rigidbody2D.velocity.x, -forwardSpeed, forwardSpeed);
    var rigidbody2D.velocity.x = Mathf.Clamp(, -forwardSpeed, forwardSpeed); 
// i dont use JS anymore so i can't say which of the above u need but atleast 1 should work.
// c# 
    float f = Mathf.Clamp(rigidbody2D.velocity.x, -forwardSpeed, forwardSpeed);