Example #1
0
 private Constraint GetOrFindConstraint(WheelLocation location, string postfix, Constraint[] cache)
 {
     if (cache[(int)location] == null)
     {
         cache[(int)location] = FindChild <Constraint>(location.ToString() + postfix);
     }
     return(cache[(int)location]);
 }
Example #2
0
 private RigidBody GetOrFindWheel(WheelLocation location)
 {
     if (m_wheelBodies[(int)location] == null)
     {
         m_wheelBodies[(int)location] = FindChild <RigidBody>(location.ToString() + "Tire");
     }
     return(m_wheelBodies[(int)location]);
 }
Example #3
0
        private TwoBodyTire GetOrCreateTireModel(WheelLocation location)
        {
            if (m_tireModels == null || m_tireModels.Length == 0)
            {
                m_tireModels = new TwoBodyTire[4] {
                    null, null, null, null
                };
                var tireModels = GetComponents <TwoBodyTire>();
                if (tireModels.Length == 4)
                {
                    foreach (WheelLocation wl in Enum.GetValues(typeof(WheelLocation)))
                    {
                        var trb = GetOrFindWheel(wl);
                        var rrb = FindChild <RigidBody>(wl.ToString() + "Rim");
                        m_tireModels[(int)wl] = tireModels.FirstOrDefault(tireModel => tireModel.TireRigidBody == trb &&
                                                                          tireModel.RimRigidBody == rrb);
                    }
                }
                else if (tireModels.Length != 0)
                {
                    Debug.LogWarning("Tire models mismatch: Got " + tireModels.Length + ", expecting 0 or 4.", this);
                }
            }

            var iLocation = (int)location;

            if (m_tireModels[iLocation] != null)
            {
                return(m_tireModels[iLocation]);
            }

            var tireRigidBody = GetOrFindWheel(location);
            var rimRigidBody  = FindChild <RigidBody>(location.ToString() + "Rim");

            if (tireRigidBody == null || rimRigidBody == null)
            {
                return(null);
            }

            var locks = (from constraint in GetComponentsInChildren <Constraint>()
                         where constraint.Type == ConstraintType.LockJoint
                         select constraint).ToArray();
            var         tireLock = locks.FirstOrDefault(constraint => constraint.AttachmentPair.Match(tireRigidBody, rimRigidBody));
            TwoBodyTire tire     = gameObject.AddComponent <TwoBodyTire>();

            tire.hideFlags = HideFlags.HideInInspector;
            bool valid = false;

            if (tireLock != null)
            {
                valid = tire.Configure(tireLock, tireRigidBody);
            }
            else
            {
                valid = tire.SetTire(tireRigidBody);
                valid = valid && tire.SetRim(rimRigidBody);
            }
            if (!valid)
            {
                DestroyImmediate(tire);
                return(null);
            }

            m_tireModels[iLocation] = tire;

            return(tire);
        }