Midair momentum help.

I am currently using the FPSWalkerEnhanced C# script found here.

When I apply air control, the movement is the same as if the player were standing on the ground. I want to add the player’s current momentum into the formula, so that it will take a while to slow down, or incrementally change direction, as opposed to being able to stop on a dime in mid air, or shoot off in a different direction instantly.

This is the original code:

            if (airControl  playerControl) {
                moveDirection.x = inputX * speed * inputModifyFactor;
                moveDirection.z = inputY * speed * inputModifyFactor;
                moveDirection = myTransform.TransformDirection(moveDirection);
            }

Here is what I have tried (one of many):

            if (airControl  playerControl) {
		        moveDirection.x = moveDirection.x + (inputX * speed * Time.deltaTime);
		        moveDirection.z = moveDirection.z + (inputY * speed * Time.deltaTime);
		        moveDirection = myTransform.TransformDirection(moveDirection);
            }

All I end up with are some weird jerky movements in the wrong direction, like the script is not using the player’s transform, but the world’s. I have been fighting this for hours now, and am pretty frustrated. Any help would be greatly appreciated, and will deserve a special spot in the credits once the game is done. :wink:

What you should try and do is add the velocity of the previous FixedUpdate.
Cause this is independent of the player input, it needs to be outside the if statement above.

In the variable list add

public Vector3 lastVelocity;

Change your code to this

if (airControl) {
       moveDirection = lastVelocity;
       if(playerControl)
       {
                moveDirection.x += inputX * speed * inputModifyFactor;
                moveDirection.z += inputY * speed * inputModifyFactor;
                moveDirection = myTransform.TransformDirection(moveDirection);
       }
}

At the end of the fixedupdate add

lastVelocity = controller.velocity;

I dont know if this will work but it is worth trying out. Good luck.

Thank you very much Boomsma! You are the man! I am unable to move unless I use only x, and z, and leave y alone. Sadly, I only get jerky movement when I do this, not momentum. I am however closer than I was to getting it figured out.