X and Z velocity of rigidbody are opposite of actual velocity (C#)

I was making a movement system for the player in my 3D game. For some reason, the mid-air part of it doesn’t work. I tried to make an opposite force act upon the player while in the air (air resistance) so I checked the x and z velocity of it and made 90% (AirResistance variable is 0.9) of it act on the opposite direction. While debugging, I had it print the x and z velocity in the console constantly. When the player was mid-air, the player started to fly towards one direction and forwards even though the console is showing that it was going in the opposite direction and backwards. For example, if the player is going to the right and goes into mid-air, they starts flying left and forwards even though the console says it’s still going right and back faster and faster. Because the x and z velocity are opposite to what they actually are, I’m pretty sure the code thinks it has to push the player in the direction they are already going. I think it might be a problem with the rb.velocity, but I’m kinda new with this so it might be my own incompetence. Any help would be appreciated!

LocalVel = transform.InverseTransformDirection(rb.velocity);

            if (MidAir == false)
            {
                rb.AddForce(0, 0, ForwardForce * Time.deltaTime, ForceMode.VelocityChange);

                if (Input.GetKey("d"))
                {
                    rb.AddForce(SidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
                }

                if (Input.GetKey("a"))
                {
                    rb.AddForce(-SidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
                }
            }

            else
            {
                rb.AddForce(0, 0, -(LocalVel.z) * AirResistance, ForceMode.VelocityChange);
                rb.AddForce(-(LocalVel.x) * AirResistance, 0, 0, ForceMode.VelocityChange);

                if (Input.GetKey("d"))
                {
                    rb.AddForce((1 - AirResistance) * SidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
                }

                if (Input.GetKey("a"))
                {
                    rb.AddForce((1 - AirResistance) * -SidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
                }
            }

Uhm your “LocalVel.z” is the local space velocity of your object. You then use this local space velocity and apply it in worldspace coordinates by using AddForce(0,0, V). So you are mixing different coordinate spaces.

Of course your whole air resistance code is framerate bound so only use it in FixedUpdate and do not change your fixed update rate. Such non-linear manipulations can’t really be made framerate independent.

Apart from your air resistance all your controls are also applied in worldspace. So pressing “d” will accelerate your player into the worldspace x direction and not your players local x direction. You probably want to use rb.AddRelativeForce instead. Alternatively you can use the objects transform forward and right vectors to determine the proper force direction in worldspace.

So either do

if (Input.GetKey("d"))
{
    rb.AddForce(transform.right * SidewaysForce * Time.deltaTime, ForceMode.VelocityChange);
}

or use AddRelativeForce like this:

if (Input.GetKey("d"))
{
    rb.AddRelativeForce(SidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}