void Start()
 {
     sus = GetComponent <Suspension>();
 }
        void Start()
        {
            tr = transform;
            rb = (Rigidbody)F.GetTopmostParentComponent <Rigidbody>(tr);
            vp = (VehicleParent)F.GetTopmostParentComponent <VehicleParent>(tr);
            suspensionParent    = tr.parent.GetComponent <Suspension>();
            travelDist          = suspensionParent.targetCompression;
            canDetach           = detachForce < Mathf.Infinity && Application.isPlaying;
            initialTirePressure = tirePressure;

            if (tr.childCount > 0)
            {
                //Get rim
                rim = tr.GetChild(0);

                //Set up rim glow material
                if (rimGlow > 0 && Application.isPlaying)
                {
                    rimMat = new Material(rim.GetComponent <MeshRenderer>().sharedMaterial);
                    rimMat.EnableKeyword("_EMISSION");
                    rim.GetComponent <MeshRenderer>().material = rimMat;
                }

                //Create detached wheel
                if (canDetach)
                {
                    detachedWheel           = new GameObject(vp.transform.name + "'s Detached Wheel");
                    detachedWheel.layer     = LayerMask.NameToLayer("Detachable Part");
                    detachFilter            = detachedWheel.AddComponent <MeshFilter>();
                    detachFilter.sharedMesh = rim.GetComponent <MeshFilter>().sharedMesh;
                    MeshRenderer detachRend = detachedWheel.AddComponent <MeshRenderer>();
                    detachRend.sharedMaterial = rim.GetComponent <MeshRenderer>().sharedMaterial;
                    detachedCol        = detachedWheel.AddComponent <MeshCollider>();
                    detachedCol.convex = true;
                    detachedBody       = detachedWheel.AddComponent <Rigidbody>();
                    detachedBody.mass  = mass;
                }

                //Get tire
                if (rim.childCount > 0)
                {
                    tire = rim.GetChild(0);
                    if (deformAmount > 0 && Application.isPlaying)
                    {
                        tireMat = new Material(tire.GetComponent <MeshRenderer>().sharedMaterial);
                        tire.GetComponent <MeshRenderer>().material = tireMat;
                    }

                    //Create detached tire
                    if (canDetach)
                    {
                        detachedTire = new GameObject("Detached Tire");
                        detachedTire.transform.parent        = detachedWheel.transform;
                        detachedTire.transform.localPosition = Vector3.zero;
                        detachedTire.transform.localRotation = Quaternion.identity;
                        detachTireFilter            = detachedTire.AddComponent <MeshFilter>();
                        detachTireFilter.sharedMesh = tire.GetComponent <MeshFilter>().sharedMesh;
                        MeshRenderer detachTireRend = detachedTire.AddComponent <MeshRenderer>();
                        detachTireRend.sharedMaterial = tireMat ? tireMat : tire.GetComponent <MeshRenderer>().sharedMaterial;
                    }
                }

                if (Application.isPlaying)
                {
                    //Generate hard collider
                    if (generateHardCollider)
                    {
                        GameObject sphereColNew = new GameObject("Rim Collider");
                        sphereColNew.layer        = GlobalControl.ignoreWheelCastLayer;
                        sphereColTr               = sphereColNew.transform;
                        sphereCol                 = sphereColNew.AddComponent <SphereCollider>();
                        sphereColTr.parent        = tr;
                        sphereColTr.localPosition = Vector3.zero;
                        sphereColTr.localRotation = Quaternion.identity;
                        sphereCol.radius          = Mathf.Min(rimWidth * 0.5f, rimRadius * 0.5f);
                        sphereCol.material        = GlobalControl.frictionlessMatStatic;
                    }

                    if (canDetach)
                    {
                        detachedWheel.SetActive(false);
                    }
                }
            }

            targetDrive = GetComponent <DriveForce>();
            currentRPM  = 0;
        }
Esempio n. 3
0
        public override void OnInspectorGUI()
        {
            GUIStyle boldFoldout = new GUIStyle(EditorStyles.foldout);

            boldFoldout.fontStyle = FontStyle.Bold;
            Suspension targetScript = (Suspension)target;

            Suspension[] allTargets = new Suspension[targets.Length];
            isPrefab = PrefabUtility.GetPrefabType(targetScript) == PrefabType.Prefab;

            for (int i = 0; i < targets.Length; i++)
            {
                Undo.RecordObject(targets[i], "Suspension Change");
                allTargets[i] = targets[i] as Suspension;
            }

            if (!targetScript.wheel)
            {
                EditorGUILayout.HelpBox("Wheel must be assigned.", MessageType.Error);
            }

            DrawDefaultInspector();

            if (!isPrefab && targetScript.gameObject.activeInHierarchy)
            {
                showButtons = EditorGUILayout.Foldout(showButtons, "Quick Actions", boldFoldout);
                EditorGUI.indentLevel++;
                if (showButtons)
                {
                    if (GUILayout.Button("Get Wheel"))
                    {
                        foreach (Suspension curTarget in allTargets)
                        {
                            curTarget.wheel = curTarget.transform.GetComponentInChildren <Wheel>();
                        }
                    }

                    if (GUILayout.Button("Get Opposite Wheel"))
                    {
                        foreach (Suspension curTarget in allTargets)
                        {
                            VehicleParent vp         = (VehicleParent)F.GetTopmostParentComponent <VehicleParent>(curTarget.transform);
                            Suspension    closestOne = null;
                            float         closeDist  = Mathf.Infinity;

                            foreach (Wheel curWheel in vp.wheels)
                            {
                                float curDist = Vector2.Distance(new Vector2(curTarget.transform.localPosition.y, curTarget.transform.localPosition.z), new Vector2(curWheel.transform.parent.localPosition.y, curWheel.transform.parent.localPosition.z));
                                if (Mathf.Sign(curTarget.transform.localPosition.x) != Mathf.Sign(curWheel.transform.parent.localPosition.x) && curDist < closeDist)
                                {
                                    closeDist  = curDist;
                                    closestOne = curWheel.transform.parent.GetComponent <Suspension>();
                                }
                            }

                            curTarget.oppositeWheel = closestOne;
                        }
                    }
                }
                EditorGUI.indentLevel--;
            }

            if (GUI.changed)
            {
                EditorUtility.SetDirty(targetScript);
            }
        }