コード例 #1
0
    /// <summary>
    /// Raises the trigger exit 2d event. When two slidebars end connection with one another.
    /// Sets this slidebar's min and max, and if this slidebar contains a platform, sets
    /// the platforms min and max movement (which is none)
    /// </summary>
    /// <param name="collider">Collider.</param>
    void OnTriggerExit2D(Collider2D collider)
    {
        findOrientation();

        //if we are leaving contact with another slidebar
        if (collider.gameObject.CompareTag("SlideBar"))
        {
            //set to null because we're no longer connected
            connectedSlideBar = null;

            //mins and maxes are our own
            if (myOrientation == Orientation.Horizontal)
            {
                min = myCollider.bounds.min.x;
                max = myCollider.bounds.max.x;
            }
            else
            {
                min = myCollider.bounds.min.y;
                max = myCollider.bounds.max.y;
            }

            //if we contain a platform
            if (platform != null)
            {
                //get the platform's script
                PlatformDrag platformScript = platform.GetComponent <PlatformDrag> ();
                //platform can no longer slide
                platformScript.CanSlide(false);

                //set the platform's min and max movement to our own
                if (myOrientation == Orientation.Horizontal)
                {
                    platformScript.setSlideMin(myCollider.bounds.min.x - (collider.transform.localScale.x / 2.0f));
                    platformScript.setSlideMax(myCollider.bounds.min.x - (collider.transform.localScale.x / 2.0f));
                }
                else
                {
                    platformScript.setSlideMin(myCollider.bounds.min.y - (collider.transform.localScale.y / 2.0f));
                    platformScript.setSlideMax(myCollider.bounds.min.y - (collider.transform.localScale.y / 2.0f));
                }
            }
        }
    }
コード例 #2
0
    /// <summary>
    /// Raises the trigger enter2 d event. When two slidebars start connection with one another.
    /// Sets this slidebar's fields to reflect its connected slidebar and if this slidebar contains
    /// a platform, sets the min and max movement of the platform.
    /// </summary>
    /// <param name="collider">Collider.</param>
    void OnTriggerEnter2D(Collider2D collider)
    {
        //if this slidebar has a platform attached
        if (platform != null)
        {
            //and this collision is with another slidebar
            if (collider.transform.tag == "SlideBar")
            {
                findOrientation();

                //script of the attached platform
                PlatformDrag platformScript = platform.GetComponent <PlatformDrag> ();

                //set the connected slidebar
                connectedSlideBar = collider.GetComponent <SlidebarConnector> ();

                //if the connected slidebar has a platform already, we can't slide, so just return
                if (connectedSlideBar.platform != null)
                {
                    return;
                }

                //the end of the connect slidebar
                connectedPlatformSlot = connectedSlideBar.platformSlot;
                //tell the other slidebar that it's connected to this one
                connectedSlideBar.connectedSlideBar     = GetComponent <SlidebarConnector> ();
                connectedSlideBar.connectedPlatformSlot = platformSlot;
                platformScript.CanSlide(true);
                if (myOrientation == Orientation.Horizontal)
                {
                    //if our min is smaller, then our min is lowest the platform can slide, and his max is the max it can slide
                    if (platformSlot.transform.position.x < connectedPlatformSlot.transform.position.x)
                    {
                        min = platformSlot.transform.position.x;
                        max = connectedPlatformSlot.transform.position.x;
                    }
                    else
                    {
                        min = connectedPlatformSlot.transform.position.x;
                        max = platformSlot.transform.position.x;
                    }
                }
                else
                {
                    //same but for y direction if we're vertical
                    if (platformSlot.transform.position.y < connectedPlatformSlot.transform.position.y)
                    {
                        min = platformSlot.transform.position.y;
                        max = connectedPlatformSlot.transform.position.y;
                    }
                    else
                    {
                        min = connectedPlatformSlot.transform.position.y;
                        max = platformSlot.transform.position.y;
                    }
                }

                //set the platform's min and max slide distances
                platformScript.setSlideMax(max);
                platformScript.setSlideMin(min);
            }
        }
    }