void OnCollisionEnter(Collision collision)
        {
            if (ParentTail == null)
            {
                GameObject.Destroy(this);
                return;
            }

            TailCollisionHelper helper = collision.transform.GetComponent <TailCollisionHelper>();

            if (helper)
            {
                if (ParentTail.CollideWithOtherTails == false)
                {
                    return;
                }
                if (helper.ParentTail == ParentTail)
                {
                    return;
                }
            }

            if (ParentTail._TransformsGhostChain.Contains(collision.transform))
            {
                return;
            }
            if (ParentTail.IgnoredColliders.Contains(collision.collider))
            {
                return;
            }

            ParentTail.CollisionDetection(Index, collision);
            previousCollision = collision.transform;
        }
        void OnTriggerEnter2D(Collider2D other)
        {
            TailCollisionHelper helper = other.transform.GetComponent <TailCollisionHelper>();

            if (other.isTrigger)
            {
                return;
            }

            if (helper)
            {
                if (ParentTail.CollideWithOtherTails == false)
                {
                    return;
                }
                if (helper.ParentTail == ParentTail)
                {
                    return;
                }
            }

            if (ParentTail._TransformsGhostChain.Contains(other.transform))
            {
                return;
            }
            if (ParentTail.IgnoredColliders2D.Contains(other))
            {
                return;
            }

            if (other is CompositeCollider2D)
            {
                return;
            }
#if UNITY_2019_4_OR_NEWER
            if (other is TilemapCollider2D)
            {
                return;
            }
#endif
            if (other is EdgeCollider2D)
            {
                return;
            }

            ParentTail.AddCollider(other);
        }
        void OnTriggerEnter(Collider other)
        {
            TailCollisionHelper helper = other.transform.GetComponent <TailCollisionHelper>();

            if (other.isTrigger)
            {
                return;
            }

            if (helper)
            {
                if (ParentTail.CollideWithOtherTails == false)
                {
                    return;
                }
                if (helper.ParentTail == ParentTail)
                {
                    return;
                }
            }

            if (ParentTail._TransformsGhostChain.Contains(other.transform))
            {
                return;
            }
            if (ParentTail.IgnoredColliders.Contains(other))
            {
                return;
            }

            if (ParentTail.IgnoreMeshColliders)
            {
                if (other is MeshCollider)
                {
                    return;
                }
            }

            ParentTail.AddCollider(other);
        }
Esempio n. 4
0
        /// <summary>
        /// Generating colliders on tail with provided settings
        /// If collision space is world then rigidbody colliders are added, if selective no additional components are needed
        /// </summary>
        void SetupSphereColliders()
        {
            if (CollisionSpace == ECollisionSpace.World_Slow)
            {
                for (int i = 1; i < _TransformsGhostChain.Count; i++)
                {
                    if (CollidersSameLayer)
                    {
                        _TransformsGhostChain[i].gameObject.layer = gameObject.layer;
                    }
                    else
                    {
                        _TransformsGhostChain[i].gameObject.layer = CollidersLayer;
                    }
                }

                if (CollidersType != 0)
                {
                    for (int i = 1; i < _TransformsGhostChain.Count - 1; i++)
                    {
                        CapsuleCollider caps = _TransformsGhostChain[i].gameObject.AddComponent <CapsuleCollider>();

                        TailCollisionHelper tcol = _TransformsGhostChain[i].gameObject.AddComponent <TailCollisionHelper>().Init(CollidersAddRigidbody, RigidbodyMass);
                        tcol.TailCollider = caps;
                        tcol.Index        = i;
                        tcol.ParentTail   = this;

                        caps.radius    = GetColliderSphereRadiusFor(_TransformsGhostChain, i);
                        caps.direction = 2;
                        caps.height    = (_TransformsGhostChain[i].position - _TransformsGhostChain[i + 1].position).magnitude * 2f - caps.radius;
                        caps.center    = _TransformsGhostChain[i].InverseTransformPoint(Vector3.Lerp(_TransformsGhostChain[i].position, _TransformsGhostChain[i + 1].position, 0.5f));

                        TailSegments[i].ColliderRadius  = caps.radius;
                        TailSegments[i].CollisionHelper = tcol;
                    }
                }
                else
                {
                    for (int i = 1; i < _TransformsGhostChain.Count; i++)
                    {
                        SphereCollider      s    = _TransformsGhostChain[i].gameObject.AddComponent <SphereCollider>();
                        TailCollisionHelper tcol = _TransformsGhostChain[i].gameObject.AddComponent <TailCollisionHelper>().Init(CollidersAddRigidbody, RigidbodyMass);
                        tcol.TailCollider = s;
                        tcol.Index        = i;
                        tcol.ParentTail   = this;
                        s.radius          = GetColliderSphereRadiusFor(_TransformsGhostChain, i);
                        TailSegments[i].ColliderRadius  = s.radius;
                        TailSegments[i].CollisionHelper = tcol;
                    }
                }
            }
            else
            {
                for (int i = 0; i < _TransformsGhostChain.Count; i++)
                {
                    TailSegments[i].ColliderRadius = GetColliderSphereRadiusFor(i);
                }

                IncludedCollidersData = new List <FImp_ColliderData_Base>();
                CollidersDataToCheck  = new List <FImp_ColliderData_Base>();

                #region Dynamic inclusion preparations

                if (DynamicWorldCollidersInclusion)
                {
                    if (CollisionMode == ECollisionMode.m_3DCollision)
                    {
                        for (int i = 0; i < IncludedColliders.Count; i++)
                        {
                            DynamicAlwaysInclude.Add(IncludedColliders[i]);
                        }
                    }
                    else
                    {
                        for (int i = 0; i < IncludedColliders2D.Count; i++)
                        {
                            DynamicAlwaysInclude.Add(IncludedColliders2D[i]);
                        }
                    }

                    Transform middleSegm = TailSegments[TailSegments.Count / 2].transform;
                    float     scaleRef   = Vector3.Distance(_TransformsGhostChain[0].position, _TransformsGhostChain[_TransformsGhostChain.Count - 1].position);

                    TailCollisionHelper cHelper = middleSegm.gameObject.AddComponent <TailCollisionHelper>();
                    cHelper.ParentTail = this;
                    SphereCollider   triggerC   = null;
                    CircleCollider2D triggerC2D = null;

                    if (CollisionMode == ECollisionMode.m_3DCollision)
                    {
                        triggerC             = middleSegm.gameObject.AddComponent <SphereCollider>();
                        triggerC.isTrigger   = true;
                        cHelper.TailCollider = triggerC;
                    }
                    else
                    {
                        triggerC2D             = middleSegm.gameObject.AddComponent <CircleCollider2D>();
                        triggerC2D.isTrigger   = true;
                        cHelper.TailCollider2D = triggerC2D;
                    }

                    cHelper.Init(true, 1f, true);

                    float scale = Mathf.Abs(middleSegm.transform.lossyScale.z);
                    if (scale == 0f)
                    {
                        scale = 1f;
                    }
                    if (triggerC != null)
                    {
                        triggerC.radius = scaleRef / scale;
                    }
                    else
                    {
                        triggerC2D.radius = scaleRef / scale;
                    }

                    if (CollidersSameLayer)
                    {
                        middleSegm.gameObject.layer = gameObject.layer;
                    }
                    else
                    {
                        middleSegm.gameObject.layer = CollidersLayer;
                    }
                }

                #endregion

                RefreshIncludedCollidersDataList();
            }

            collisionInitialized = true;
        }