Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        // Animation
        anim.SetBool("Moving", Input.GetAxis("Horizontal") != 0 ? true : false);
        anim.SetBool("OnGround", cc2D.onGround());
        anim.SetBool("IsSliding", isWallsliding);

        // Animation rotation
        if (Input.GetAxis("Horizontal") < -0.1)
        {
            isFacingRight = false;
        }
        else
        if (Input.GetAxis("Horizontal") > 0.1)
        {
            isFacingRight = true;
        }

        transform.Find("sprite").transform.rotation = Quaternion.Euler(0, 180 * ((isFacingRight == true) ? 0 : 1), 0);

        // Movement
        movement = new Vector3(velocity.x + (Input.GetAxis("Horizontal") * (MaxMovementspeed * MovementspeedBuff)), movement.y, 0);
        movement = transform.TransformDirection(movement);

        // Gravity
        if (!cc.isGrounded)
        {
            movement.y += Physics.gravity.y * Time.deltaTime;
        }
        else
        {
            movement.y = 0;
        }

        // Velocity
        if (velocity.x >= 1)
        {
            velocity.x -= 60 * Time.deltaTime;
        }
        else
        if (velocity.x <= -1)
        {
            velocity.x += 60 * Time.deltaTime;
        }
        else
        {
            velocity.x = 0;
        }


        // Jumping
        if (Input.GetButton("Jump"))
        {
            if (cc.isGrounded && Time.time >= NextJump)              // Normal jump

            // Play jump sound
            {
                Audio.PlayOneShot(JumpSound, 0.03f);
                movement.y = JumpHeight;
                NextJump   = Time.time + JumpRate;
            }
        }

        // Landing
        if (!cc2D.onGround())
        {
            isInAir          = true;
            LastJumpVelocity = movement.y;
        }

        if (cc2D.onGround() && isInAir)
        {
            isInAir = false;
            mCam.Shake(.2f, LastJumpVelocity / 100);
        }

        // Walljumping

        // Get what wall we are next to
        string NextToWall = cc2D.checkForJumpDirection().ToString();

        // Confirm if we can do a walljump
        if ((NextToWall == "Right" || NextToWall == "Left") && Input.GetButtonDown("Jump") && !cc.isGrounded)
        {
            int JumpMultiplier = (NextToWall == "Right") ? 1 : -1;

            // Check if player can jump
            if (Time.time >= NextJump)
            {
                // Play jump sound
                Audio.PlayOneShot(JumpSound, 0.03f);

                // Wallhop
                if ((Input.GetAxis("Horizontal") == 1 && NextToWall == "Right") || (Input.GetAxis("Horizontal") == -1 && NextToWall == "Left"))
                {
                    Debug.Log("Do wall hop");
                    velocity.x = (JumpHop.x * -JumpMultiplier) * MovementspeedBuff;
                    movement.y = JumpHop.y;
                }
                else
                { // Normal jump
                    Debug.Log("Do normal jump");
                    velocity.x = (JumpJump.x * -JumpMultiplier) * MovementspeedBuff;
                    movement.y = JumpJump.y;
                }

                NextJump = Time.time + JumpRate;
            }
        }

        // Head bump
        if (cc2D.HitHead())     // Reset the velocity if the head is hit
        {
            movement.y = -0.9f; // Reset velocity
        }


        // Wallsliding
        if ((Input.GetAxis("Horizontal") == 1 && NextToWall == "Right") || (Input.GetAxis("Horizontal") == -1 && NextToWall == "Left") && movement.y < 0 && !cc.isGrounded)
        {
            if (movement.y < -MaxWallslidingspeed)
            {
                movement.y = -MaxWallslidingspeed;
            }
        }

        // Wallsliding for animations
        if ((Input.GetAxis("Horizontal") == 1 && NextToWall == "Right") || (Input.GetAxis("Horizontal") == -1 && NextToWall == "Left"))
        {
            isWallsliding = true;
        }

        if (!((Input.GetAxis("Horizontal") == 1 && NextToWall == "Right") || (Input.GetAxis("Horizontal") == -1 && NextToWall == "Left")))
        {
            isWallsliding = false;
        }


        // Move the player through the character controller
        cc.Move(movement * Time.deltaTime);

        // Click on Left mouse button
        if (Input.GetMouseButton(0) && Time.time >= NextFire)
        {
            NextFire = Time.time + FireRate;
            FirePaint();
        }
    }