Example #1
0
        void Update()
        {
            Vector2 input    = new Vector2(GamepadInput.HorizontalVal(), 0);
            int     wallDirX = (controller.collisions.left) ? -1 : 1;

            float targetVelocityX = input.x * moveSpeed;

            //if (ZeroGravity)
            //{
            //    if (Mathf.Abs(velocity.x) < 0.0001f)
            //    {
            //        velocity.x = Mathf.Abs(1 * Mathf.Sign(velocity.x)) < 0.0001f ? 1 : Mathf.Sign(velocity.x);
            //    }
            //}
            //else
            //{
            if (!ZeroGravity)
            {
                velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing,
                                              (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne);
            }
            //}

            bool wallSliding = false;

            if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && !ZeroGravity)
            {
                wallSliding = true;

                if (velocity.y < -wallSlideSpeedMax)
                {
                    velocity.y = -wallSlideSpeedMax;
                }

                if (timeToWallUnstick > 0)
                {
                    velocityXSmoothing = 0;
                    velocity.x         = 0;

                    if (input.x != wallDirX && input.x != 0)
                    {
                        timeToWallUnstick -= Time.deltaTime;
                    }
                    else
                    {
                        timeToWallUnstick = wallStickTime;
                    }
                }
                else
                {
                    timeToWallUnstick = wallStickTime;
                }
            }

            if ((controller.collisions.above || controller.collisions.below) && !ZeroGravity)
            {
                velocity.y = 0;
            }

            if (controller.collisions.below)
            {
                canDash = true;
            }

            if (GamepadInput.Jump())
            {
                if (wallSliding)
                {
                    velocity.x = -wallDirX * wallLeap.x;
                    velocity.y = wallLeap.y;
                }
                if (controller.collisions.below)
                {
                    //GameManager.instance.playJumpSound();
                    velocity.y = jumpVelocity;
                }
            }

            if (GamepadInput.Dash() && canDash && input.normalized.x != 0)
            {
                //GameManager.instance.playDashSound();
                velocity.x = input.normalized.x * dash;
                velocity.y = 7; //very importante
                canDash    = false;
            }


            if (!ZeroGravity)
            {
                velocity.y += gravity * Time.deltaTime;
            }

            controller.Move(velocity * Time.deltaTime, input);
        }