Пример #1
0
    void HorizontalMovement()
    {
        float xInput = playerInput.getAxis(xMovementAxis);

        if (keyboardControls)
        {
            if (Input.GetKey(rightMovement))
            {
                xInput = 1;
            }
            else if (Input.GetKey(leftMovement))
            {
                xInput = -1;
            }
            else
            {
                xInput = 0;
            }
        }

        //Check for deadzone to not move around on random input
        if (xInput > playerInput.horizontalStickDeadzone || xInput < -playerInput.horizontalStickDeadzone)
        {
            // 1 is to the right, -1 is to the left
            direction = Mathf.Sign(xSpeed);

            //Did we switch directions?
            bool switchedDirections = Mathf.Sign(xInput) != Mathf.Sign(direction);
            if (xSpeed < directionSwitchLowerSpeedLimit && xSpeed > -directionSwitchLowerSpeedLimit)
            {
                switchedDirections = false;
            }

            //Set speed with acceleration
            float targetSpeed = movespeed * Mathf.Sign(xInput);
            if (!switchedDirections)
            {
                if (raycastCollisionChecks.OnGround())
                {
                    //Normal acceleration
                    xSpeed = Mathf.SmoothDamp(xSpeed, targetSpeed, ref accelerationSmooth, accelerationTime);
                }
                else
                {
                    //Normal acceleration in air
                    xSpeed = Mathf.SmoothDamp(xSpeed, targetSpeed, ref airAccelerationSmooth, airAccelerationTime);
                }
            }
            else
            {
                if (raycastCollisionChecks.OnGround())
                {
                    //Acceleration on direction switch
                    xSpeed = Mathf.SmoothDamp(xSpeed, targetSpeed, ref directionSwitchSmooth, directionSwitchTime);
                }
                else
                {
                    //Acceleration on direction switch in air
                    xSpeed = Mathf.SmoothDamp(xSpeed, targetSpeed, ref airDirectionSwitchSmooth, airDirectionSwitchTime);
                }
            }
        }
        else
        {
            if (raycastCollisionChecks.OnGround())
            {
                //Decceleration on no input
                xSpeed = Mathf.SmoothDamp(xSpeed, 0.0f, ref deccelerationSmooth, deccelerationTime);
            }
            else
            {
                //Decceleration on no input
                xSpeed = Mathf.SmoothDamp(xSpeed, 0.0f, ref airDeccelerationSmooth, airDeccelerationTime);
            }
        }

        if (!disableMoving)
        {
            rigidbody2d.velocity = new Vector2(xSpeed, rigidbody2d.velocity.y);
        }
    }