Ejemplo n.º 1
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
        //Needed for walking baddies otherwise they'll instantly flip.
        if (collision.collider.tag == "Ground")
        {
            FlyVelocity = -FlyVelocity;
            Rend.flipX  = !Rend.flipX;
        }

        if (collision.collider.tag == "Player")
        {
            if (Up.Collided())
            {
                if (!Dead)
                {
                    GetComponentInParent <SpriteAnim>().Stop();
                    GameObject.Find("Score").GetComponent <Score>().UpdateScore(100);
                    Dead              = true;
                    DeathStart        = Time.time;
                    Rigid.velocity    = new Vector2(0, Rigid.velocity.y);
                    Rigid.constraints = RigidbodyConstraints2D.FreezeRotation;
                    Rend.sprite       = DeadSprite;
                }
            }
        }
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        //Needed for walking baddies otherwise they'll instantly flip.
        if (FirstCollisionOver)
        {
            if (collision.collider.tag == "Ground")
            {
                SlimeVelocity = -SlimeVelocity;
                Rend.flipX    = !Rend.flipX;
            }
        }
        else
        {
            FirstCollisionOver = true;
        }

        if (collision.collider.tag == "Player")
        {
            if (Up.Collided())
            {
                if (!Dead)
                {
                    GetComponent <BoxCollider2D>().size = new Vector2(GetComponent <BoxCollider2D>().size.x, 0.1850187f);
                    GetComponentInParent <SpriteAnim>().Stop();
                    GameObject.Find("Score").GetComponent <Score>().UpdateScore(50);
                    Dead           = true;
                    DeathStart     = Time.time;
                    Rigid.velocity = new Vector2(0, Rigid.velocity.y);
                    Rend.sprite    = DeadSprite;
                }
            }
        }
    }
Ejemplo n.º 3
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetAxis("Horizontal") <= -0.9)
        {
            JoyLeftActive = true;
        }
        else
        {
            JoyLeftActive = false;
        }
        if (Input.GetAxis("Horizontal") >= 0.9)
        {
            JoyRightActive = true;
        }
        else
        {
            JoyRightActive = false;
        }

        if (Input.GetKeyDown(SprintButton))
        {
            MovementSpeed = SetSpeed + SprintBoost;
        }

        if (Input.GetKeyUp(SprintButton))
        {
            MovementSpeed = SetSpeed;
        }

        if (LeftKeyActive || RightKeyActive)
        {
            if (WalkAnimActive && !Anim.Active())
            {
                Debug.Log("AnimStart");
                Anim.PlayAnimation(0, AnimSpeed);
            }
        }

        PlayerRigid.velocity = new Vector2(Mathf.Clamp(PlayerRigid.velocity.x, -MovementSpeed, MovementSpeed), Mathf.Clamp(PlayerRigid.velocity.y, -9999, 6));

        if (Input.GetKey(LeftButton) || JoyLeftActive)
        {
            LeftKeyActive = true;
            if (MovementActive && LeftMovementActive)
            {
                PlayerRigid.velocity = new Vector2(-MovementSpeed, PlayerRigid.velocity.y);
            }

            //Flips the sprite so that it reflects the direction the player is facing.
            PlayerRenderer.flipX = true;
        }
        else
        {
            LeftKeyActive = false;
        }


        if (Input.GetKey(RightButton) || JoyRightActive)
        {
            RightKeyActive = true;
            if (MovementActive && RightMovementActive)
            {
                PlayerRigid.velocity = new Vector2(MovementSpeed, PlayerRigid.velocity.y);
            }

            //Flips the sprite so that it reflects the direction the player is facing.
            PlayerRenderer.flipX = false;
        }
        else
        {
            RightKeyActive = false;
        }

        if (!LeftKeyActive && !RightKeyActive)
        {
            //Stops the player from moving (sliding due to physics)
            Anim.Stop();
            PlayerRigid.velocity = new Vector2(0, PlayerRigid.velocity.y);

            if (CanJump)
            {
                PlayerRenderer.sprite = NormalSprite;
            }
        }

        if ((Input.GetKeyDown(JumpButton) || Input.GetButton("Jump")) && CanJump)
        {
            Anim.Stop();
            WalkAnimActive        = false;
            CanJump               = false;
            PlayerRenderer.sprite = JumpSprite;
            PlayerRigid.AddForce(new Vector2(0, JumpForce));
        }

        if (Left.Collided())
        {
            LeftMovementActive = false;
        }
        else
        {
            LeftMovementActive = true;
        }

        if (Right.Collided())
        {
            RightMovementActive = false;
        }
        else
        {
            RightMovementActive = true;
        }
    }