I am having a problem with limiting the velocity of my player, normally you would just cap the speed of the rigidbody altogether by applying dampening but I am making a game where the player will be in the air a lot and I want them to fly very fast.

After some searching I found the solution seeing if the linear velocity of the player is above the max velocity in the x/z axis and make the acceleration zero in respective axis. This works perfectly accept for the fact that going diagonaly makes the player ba able to go faster than normal.

So I have devised a solution where I have a vector in the same direction as my current horizontal speed but the length of my max speed, and for all four axies I compare the x/z with respective x/y of the max speed vector so when going diagonaly my max speed on both x/z is both ≈0.707.

Again, this solution good until I realized that while in the air, while going straight , I was locked into the very side of the max speed cirkle and needed to "back out" if I wanted to go more diagonaly. (I have made a sketch to show what I think is happening)

Sketch:

Is there an easier way to do this or am I misunderstanding something huge? I would greatly appreciate some help.

Code:

spaceyjase I have tried it, if you want me to limit the velocity directly, that does not fix the issue with wanting to be able to fly through the air faster than the max speed just not to accelerate even more. And limiting the length of the desried movement vector makes no sense unless you want me to limit it to zero which is the same as setting it to zero.

    14 days later

    crumbthecrumb

    " to be able to fly through the air faster than the max speed just not to accelerate even more.. "
    If you are using the physics apply force functions the natural way to limit velocity on the ground is a friction/damping variable. I think you should just add a check to see if they are not on the ground, and remove the damping when in the air.

    var localLinearVelocity = Vector2(linear_velocity.x, linear_velocity.z).rotated(head.rotation.y)
    I cannot understand why anyone would do this?
    linear velocity is your local velocity? why are we going from 3d to 2d?
    this would be better: head_velocity = head.global_transform.basis * linear_velocity
    making movement decisions based on head orientation doesn't make sense to me,

    maxspeedVector should be a length check of the locallinearvelocity

    # test velocity is greater then limit
    if self.linear_velocity.length() < max_velocity:
      apply_central_force( normalized_movement_direction * force )