Example #1
0
    /// <summary>
    /// Executes when the player is moving through the air.
    /// </summary>
    /// <param name="playerVelocity"></param>
    public Vector3 AirMove(Vector3 playerVelocity)
    {
        Vector3 wishDir;          //the directional input the player is trying to make this frame.

        float acceleration;       //the acceleration for this frame to be applied at the end of the frame.

        manager.SetMovementDir(); //set the movement direction based on wish direction.
        float scale = manager.CmdScale(false, moveSpeed, moveScale, airMoveSpeed);

        wishDir = new Vector3(manager.GetCmdRight(), 0, manager.GetCmdForward()); //get the desired direction of movement based on player input using the axis of input.
        wishDir = transform.TransformDirection(wishDir);                          //translates the local transform direction of x,y, and z into a world space direction. So instead of the wish direction referring to only the local transform of the player character it now refers to the world space the character resides in.

        float wishSpeed = wishDir.magnitude;                                      //the magnitude or length of the wish direction is the speed we wish to apply in the given direction. It is important to note that this speed is non-normalized, so when moving in the air we are not multiplying our speed of movement by 1 in a given direction, but on the ground this wish speed is always normalized by this point.

        wishSpeed *= airMoveSpeed;                                                //apply the air move speed function to increase speed in the given direciton.

        wishDir.Normalize();                                                      //wish direction no longer needs a magnitude as it is now just a placeholder for the direction we want to apply speed.
        moveDirectionNorm = wishDir;                                              //the move direction normal is now equal to the normalized wish direction.
        wishSpeed        *= scale;                                                //wishSpeed applies model scale.

        //CPM AirControl
        float wishSpeed2 = wishSpeed; //used for calculating the players air control, given that we apply acceleration using wishSpeed before we look at the players control possibilites.

        if (Vector3.Dot(playerVelocity, wishDir) < 0)
        {
            acceleration = airDecceleration; //if our wish direction is breaking the current player velocity (going in an opposite direction requiring us to slow down which happens whent he camera turns beyond 90 degrees of the current travel direction) then apply deceleration.
        }
        else
        {
            acceleration = airAcceleration; //Otherwise we want to accelerate within a 180 degree radius of the direction we are currently facing.
        }

        //if the player is only strafing left or right
        if (manager.GetCmdForward() == 0 && manager.GetCmdRight() != 0)
        {
            if (wishSpeed > sideStrafeSpeed)
            {
                wishSpeed = sideStrafeSpeed; //clamp wish speed while strafing
            }
            acceleration = sideStrafeSpeed;  //we can only accelerate using side strafe speed if we are soully moving left or right.
        }

        playerVelocity = Accelerate(wishDir, wishSpeed, acceleration, playerVelocity); //call the accelerate function to apply the acceleration for this frame, and clip any excess accleration off of the value we just calculated for our given wihs direction.
        if (airControl > 0)
        {
            playerVelocity = AirControl(wishDir, wishSpeed2, playerVelocity);
        }
        //!CPM: Aircontrol

        //Apply gravity
        playerVelocity.y -= gravity * Time.deltaTime; //add gravity to the player at the end of this calculation.

        return(playerVelocity);
    }