Exemple #1
0
 // Update is called once per frame
 void LateUpdate()
 {
     // call late update for each instance in order
     foreach (GameObject obj in cactusClones)
     {
         CactusBehaviour behaviour = obj.GetComponent <CactusBehaviour>();
         behaviour.LateUpdateMe();
     }
 }
    private void CollisionWithPlayer(Vector2 center, BoxCollider2D myCollider)
    {
        if (platform != null)         // do not need to check for collision when a passenger of a "platform"
        {
            return;
        }

        // need to sort the colliders
        Collider2D[] colliders = Physics2D.OverlapBoxAll(center, GetComponent <BoxCollider2D>().size, 0, 1 << LayerMask.NameToLayer("Cactus"));
        foreach (Collider2D collider in colliders)
        {
            if (collider.GetInstanceID() == myCollider.GetInstanceID())             // do not want to resolve collision with our own collider
            {
                continue;
            }

            CactusBehaviour cactusBehaviour   = collider.gameObject.GetComponent <CactusBehaviour>();
            Vector3         otherPrevPosition = cactusBehaviour.prevPosition;
            BoxCollider2D   otherCollider     = (BoxCollider2D)collider;
            float           colliderBottom    = myCollider.transform.position.y - myCollider.bounds.extents.y;
            float           otherColliderTop  = otherCollider.transform.position.y + otherCollider.bounds.extents.y;

            // when a collsion has occurred from the bottom
            if (prevPosition.y > otherPrevPosition.y)
            {
                // when this instance does not have a "parent" platform
                if (platform == null)
                {
                    platform   = otherCollider.gameObject;
                    platformId = cactusBehaviour.id;
                    transform.SetParent(otherCollider.gameObject.transform);
                }
                transform.Translate(0, otherColliderTop - colliderBottom, 0);                 // resolve the collision by moving away from the other collider
                vspeed = 0;
            }
        }
    }