Beispiel #1
0
    // Update is called once per frame
    void Update()
    {
        // get player input for direction and which direction they are colliding inot the wall
        Vector2 input    = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        int     wallDirX = (controller.collisions.left) ? -1 : 1;

        // currently not sliding on wall
        bool wallSliding = false;

        // check if sliding on wall
        if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0)
        {
            // currently colliding into wall
            wallSliding = true;

            // slide down wall slowly
            if (velocity.y < -wallSlideSpeedMax)
            {
                velocity.y = -wallSlideSpeedMax;
            }
        }

        // stop when hitting object from above or below
        if (controller.collisions.above || controller.collisions.below)
        {
            velocity.y = 0;
        }

        // player wants to move left
        if (Input.GetKey(moveLeft))
        {
            // check if player can sprint
            if (playerTwoStamina.currentStamina > 0 && Input.GetKey(sprint) && playerTwoStamina.fiftyStamina)
            {
                // move player faster to sprint and decrease stamina
                input.x = -sprintSpeed;
                playerTwoStamina.decreaseStamina();
            }

            // do not sprint
            else
            {
                // move player at normal speed
                input.x = -moveSpeed;

                // increase player stamina when not sprinting
                if (playerTwoStamina.currentStamina < playerTwoStamina.maxStamina)
                {
                    playerTwoStamina.increaseStamina();
                }
            }

            // face bullet to the left
            bulletRotation.eulerAngles = new Vector3(0, 0, 90);
            resetBulletDirection();
            bulletLeft = true;

            // face sowrd to the left
            swordDirection            = -1f;
            swordRotation.eulerAngles = new Vector3(0, 0, 90);

            // face player to the left
            greenPlayer.flipX = false;
        }

        // player want to move right
        else if (Input.GetKey(moveRight))
        {
            // check if player can sprint
            if (playerTwoStamina.currentStamina > 0 && Input.GetKey(sprint) && playerTwoStamina.fiftyStamina)
            {
                // move player faster to sprint and decrease stamina
                input.x = sprintSpeed;
                playerTwoStamina.decreaseStamina();
            }

            // do not sprint
            else
            {
                // move player at normal speed
                input.x = moveSpeed;

                // increase player stamina when not sprinting
                if (playerTwoStamina.currentStamina < playerTwoStamina.maxStamina)
                {
                    playerTwoStamina.increaseStamina();
                }
            }

            // face bullet to the right
            bulletRotation.eulerAngles = new Vector3(0, 0, -90);
            resetBulletDirection();
            bulletRight = true;

            // face sword to the right
            swordDirection            = 1f;
            swordRotation.eulerAngles = new Vector3(0, 0, 0);

            // face player to the right
            greenPlayer.flipX = true;
        }

        // player does not want to move
        else
        {
            // do not move player
            input.x = 0;

            // increase player stamina
            if (playerTwoStamina.currentStamina < playerTwoStamina.maxStamina)
            {
                playerTwoStamina.increaseStamina();
            }
        }

        // player wants to shoot diaginally up and right
        if (Input.GetKey(shootTopRight) && Time.time > nextFire)
        {
            // face bullet up and right
            bulletRotation.eulerAngles = new Vector3(0, 0, -45);
            resetBulletDirection();
            bulletTopRight = true;

            // create bullet
            Instantiate(bullet, transform.position, bulletRotation);

            // add cooldown to fire rate
            nextFire = Time.time + fireRate;
        }

        // player wants to shoot diaginally down and right
        else if (Input.GetKey(shootBottomRight) && Time.time > nextFire)
        {
            // face bullet down and right
            bulletRotation.eulerAngles = new Vector3(0, 0, -135);
            resetBulletDirection();
            bulletBottomRight = true;

            // create bullet
            Instantiate(bullet, transform.position, bulletRotation);

            // add cooldown to fire rate
            nextFire = Time.time + fireRate;
        }

        // player wants to shoot diaginally up and left
        else if (Input.GetKey(shootTopLeft) && Time.time > nextFire)
        {
            // face bullet up and left
            bulletRotation.eulerAngles = new Vector3(0, 0, 45);
            resetBulletDirection();
            bulletTopLeft = true;

            // create bullet
            Instantiate(bullet, transform.position, bulletRotation);

            // add cooldown to fire rate
            nextFire = Time.time + fireRate;
        }

        // player wants to shoot diaginally down and left
        else if (Input.GetKey(shootBottomLeft) && Time.time > nextFire)
        {
            // face bullet down and left
            bulletRotation.eulerAngles = new Vector3(0, 0, 135);
            resetBulletDirection();
            bulletBottomLeft = true;

            // create bullet
            Instantiate(bullet, transform.position, bulletRotation);

            // add cooldown to fire rate
            nextFire = Time.time + fireRate;
        }

        // player wants to jump
        if (Input.GetKey(jump))
        {
            // check if sliding on wall
            if (wallSliding)
            {
                // player jumps to same wall they are sliding on
                if (wallDirX == input.x)
                {
                    velocity.x = -wallDirX * wallJumpClimb.x;
                    velocity.y = wallJumpClimb.y;
                }

                // player jumps off wall
                else if (input.x == 0)
                {
                    velocity.x = -wallDirX * wallJumpOff.x;
                    velocity.y = wallJumpOff.y;
                }

                // player jumps off wall and goes in opposite direction of wall
                else
                {
                    velocity.x = -wallDirX * wallLeap.x;
                    velocity.y = wallLeap.y;
                }
            }

            // be able to jump when on ground
            if (controller.collisions.below)
            {
                velocity.y = jumpVelocity;
            }
        }

        // player wants to shoot forward
        if (Input.GetKey(shoot) && Time.time > nextFire)
        {
            // create bullet
            Instantiate(bullet, transform.position, bulletRotation);

            // add cooldown to fire rate
            nextFire = Time.time + fireRate;
        }

        // player wants to use sword
        if (Input.GetKey(useSword) && Time.time > nextFire)
        {
            // create sword
            Instantiate(sword, new Vector3(transform.position.x + swordDirection, transform.position.y, transform.position.z), swordRotation);

            // add cooldown to fire rate
            nextFire = Time.time + fireRate;
        }

        // move player
        float targetVelocityX = input.x * moveSpeed * Time.deltaTime;

        velocity.x  = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelationTimeAirborne);
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }