Пример #1
0
        private void CheckRigidbodyContacts(Rigidbody2D rb)
        {
            int contactsCount = rb.GetContacts(contactFilter, PointsOfContact);

            for (int j = 0; j < contactsCount; j++)
            {
                ContactPoint2D contactPoint2D = PointsOfContact[j];

                Rigidbody2D contactRigidbody = contactPoint2D.rigidbody == rb ? contactPoint2D.otherRigidbody : contactPoint2D.rigidbody;
                int         listIndex        = -1;

                for (int k = 0; k < ObjectsCaught.Count; k++)
                {
                    if (contactRigidbody == ObjectsCaught[k].rigidbody)
                    {
                        listIndex = k;
                        break;
                    }
                }

                if (listIndex == -1)
                {
                    if (contactRigidbody != null)
                    {
                        if (contactRigidbody.bodyType != RigidbodyType2D.Static && contactRigidbody != platformRigidbody)
                        {
                            float dot = Vector2.Dot(contactPoint2D.normal, Vector2.down);
                            if (dot > 0.8f)
                            {
                                CaughtObject newCaughtObject = new CaughtObject
                                {
                                    rigidbody        = contactRigidbody,
                                    character        = contactRigidbody.GetComponent <ICharacter>(),
                                    collider         = contactRigidbody.GetComponent <Collider2D>(),
                                    inContact        = true,
                                    checkedThisFrame = false
                                };

                                ObjectsCaught.Add(newCaughtObject);
                            }
                        }
                    }
                }
                else
                {
                    ObjectsCaught[listIndex].inContact = true;
                }
            }
        }
Пример #2
0
        public void MoveCaughtObjects(Vector2 velocity)
        {
            // For complex moving
            if (OnMovingDelegate != null)
            {
                OnMovingDelegate.Invoke(velocity);
            }

            for (int i = 0, count = ObjectsCaught.Count; i < count; i++)
            {
                CaughtObject caughtObject = ObjectsCaught[i];
                if (ParentCatcher != null && ParentCatcher.ObjectsCaught.Find((CaughtObject A) => { return(A.rigidbody == caughtObject.rigidbody); }) != null)
                {
                    continue;
                }

                ObjectsCaught[i].Move(velocity);
            }
        }
Пример #3
0
        void FixedUpdate()
        {
            for (int i = 0, count = ObjectsCaught.Count; i < count; i++)
            {
                CaughtObject caughtObject = ObjectsCaught[i];
                caughtObject.inContact        = false;
                caughtObject.checkedThisFrame = false;
            }

            CheckRigidbodyContacts(platformRigidbody);

            bool checkAgain;

            do
            {
                for (int i = 0, count = ObjectsCaught.Count; i < count; i++)
                {
                    CaughtObject caughtObject = ObjectsCaught[i];

                    if (caughtObject.inContact)
                    {
                        if (!caughtObject.checkedThisFrame)
                        {
                            CheckRigidbodyContacts(caughtObject.rigidbody);
                            caughtObject.checkedThisFrame = true;
                        }
                    }
                    // Some cases will remove all contacts (collider resize etc.) leading to loosing contact with the platform
                    // so we check the distance of the object to the top of the platform.
                    if (!caughtObject.inContact)
                    {
                        Collider2D caughtObjectCollider = ObjectsCaught[i].collider;

                        // check if we are aligned with the moving paltform, otherwise the yDiff test under would be true even if far from the platform as long as we are on the same y level...
                        bool verticalAlignement = (caughtObjectCollider.bounds.max.x > CatcherCollider.bounds.min.x) && (caughtObjectCollider.bounds.min.x < CatcherCollider.bounds.max.x);

                        if (verticalAlignement)
                        {
                            float yDiff = ObjectsCaught[i].collider.bounds.min.y - CatcherCollider.bounds.max.y;

                            if (yDiff > 0 && yDiff < 0.05f)
                            {
                                caughtObject.inContact        = true;
                                caughtObject.checkedThisFrame = true;
                            }
                        }
                    }
                }

                checkAgain = false;

                for (int i = 0, count = ObjectsCaught.Count; i < count; i++)
                {
                    CaughtObject caughtObject = ObjectsCaught[i];
                    if (caughtObject.inContact && !caughtObject.checkedThisFrame)
                    {
                        checkAgain = true;
                        break;
                    }
                }
            }while (checkAgain);
        }