Ejemplo n.º 1
0
    //This simply gives you an option to delay a line of code. In this case we are enabling Update function in the Boards <OnJumpTrigger> component.
    IEnumerator EnableAgain(OnJumpTrigger trigger)
    {
        yield return(new WaitForSeconds(0.5f));

        foreach (GameObject gob in trigger.Neighbours)
        {
            gob.GetComponent <OnJumpTrigger>().enabled = true;
        }

        StopCoroutine(EnableAgain(trigger));
    }
Ejemplo n.º 2
0
    public void Crouch(bool IsCrouching)
    {
        //If on ground while Crouch is pressed set the boolean inside animation to same value as bool IsCrouching from the functions input.
        if (IsOnGround())
        {
            anim.SetBool("IsCrouching", IsCrouching);
        }

        if (IsCrouching)
        {
            //If crouching is true, set the height of the collider to be half of set size and it's wight to be 50% wider. also lower the collider a bit.
            //This is because once he crouches in the animation his width gets .. wider. There is another way to do it, but this will suffice.
            groundBoxcollider2D.size   = new Vector2(colliderSize.x / 2, colliderSize.y * 1.5f);
            groundBoxcollider2D.offset = new Vector2(colliderOffset.x, 0f);
            //We cast a raycast to check if under is a board we can fall through.
            raycasthit2D = Physics2D.BoxCast(groundBoxcollider2D.bounds.center, groundBoxcollider2D.bounds.size, 0f, Vector2.down, 0.1f, floorLayerMask);
            if (raycasthit2D.collider != null)
            {
                if (raycasthit2D.collider.tag == "Board")
                {
                    OnJumpTrigger trigger = raycasthit2D.collider.gameObject.GetComponent <OnJumpTrigger>();
                    trigger.enabled = false;
                    foreach (GameObject gob in trigger.Neighbours)
                    {
                        gob.GetComponent <OnJumpTrigger>().enabled = false;
                    }
                    //Collider is set to be a trigger, which means we can pass through a collider and physics are ignored.
                    trigger.ChangeTriggerValue(true);
                    //Start the IEnumerator EnableAgain().
                    StartCoroutine(EnableAgain(trigger));
                }
            }
        }
        else
        {
            //If we are not crouching return the size and offset of collider to normal.
            groundBoxcollider2D.size   = colliderSize;
            groundBoxcollider2D.offset = colliderOffset;
        }
    }