コード例 #1
0
 public override void SetCar(TSTrafficAI car)
 {
     base.SetCar(car);
     car.ignoreTrafficLight = true;
     if (disableCarUntilTriggeredByPlayer)
     {
         DisableCarAI();
     }
 }
コード例 #2
0
    // Use this for initialization
    void Start()
    {
        body       = GetComponent <Rigidbody>();
        animator   = GetComponent <Animator>();
        locomotion = new Locomotion(animator);
        TSTrafficAI ai = GetComponent <TSTrafficAI>();

        ai.OnUpdateAI     = OnAIUpdate;
        ai.UpdateCarSpeed = UpdateSpeed;
    }
コード例 #3
0
    /// <summary>
    /// All wheels.
    /// </summary>
    /*PART OF EXPERIMENTAL CODE TO AUTO SWITCH FROM AND TO SUPER SIMPLE PHYSICS*/
    //	private TSSimpleCar_Wheel[] allWheels;

    void Awake()
    {
        //Get the cached references
        myTransform = transform;
        body        = GetComponent <Rigidbody>();
        bodies      = GetComponentsInChildren <Rigidbody>();
        trafficAI   = GetComponent <TSTrafficAI>();
        audioSource = GetComponent <AudioSource>();
        if (audioSource == null)
        {
            engineAudioEnabled = false;
        }

        /*PART OF EXPERIMENTAL CODE TO AUTO SWITCH FROM AND TO SUPER SIMPLE PHYSICS*/
        //		allWheels = GetComponentsInChildren<TSSimpleCar_Wheel>();
    }
コード例 #4
0
    void assignWheels()
    {
        GameObject[] selectedTires = Selection.gameObjects;
        if (selectedTires.Length > 0)
        {
            tire = new Tire[0];

            rigCarEmpty.transform.position    = Vector3.zero;
            rigCarEmpty.transform.eulerAngles = Vector3.zero;
            int i = 1;
            foreach (GameObject selectedTire in selectedTires)
            {
                Transform[] subSelectedTires = selectedTire.GetComponentsInChildren <Transform>();
                foreach (Transform tireTransform in subSelectedTires)
                {
                    Renderer tireRender = tireTransform.GetComponent <Renderer>();
                    if (tireRender)
                    {
                        System.Array.Resize <Tire>(ref tire, i);
                        tire[i - 1]             = new Tire();
                        tire[i - 1].tiresCenter = tireRender.bounds.center;
                        tire[i - 1].size        = tireRender.bounds.size;
                        tire[i - 1].tireObj     = tireTransform.gameObject;
                        tire[i - 1].wRadius     = tireRender.GetComponent <MeshFilter>().sharedMesh.bounds.extents.y;                 // * tire[i-1].tireObj.transform.localScale.y;
                        tire[i - 1].localBound  = tireRender.GetComponent <MeshFilter>().sharedMesh.bounds;
                        tire[i - 1].bound       = tireRender.bounds;
                        i++;
                    }
                }
            }
            int tireCount = 1;
            findWheelsPosition();
            System.Array.Sort(tire, tireSizeOrder);
            GameObject[] tireContainer = new GameObject[0];
            for (int r = 0; r < tire.Length; r++)
            {
                if (tire[r].tireGroup != 0)
                {
                    continue;
                }
                tire[r].bound.extents = new Vector3(tire[r].bound.extents.x * 1.5f, tire[r].bound.extents.y * 1.5f, tire[r].bound.extents.z * 1.5f);
                for (int w = 0; w < tire.Length; w++)
                {
                    bool conatins = tire[r].bound.Contains(tire[w].bound.min) && tire[r].bound.Contains(tire[w].bound.max);                     //  Mathf.Abs(tire[w].tiresCenter.z) > Mathf.Abs(tire[r].tiresCenter.z) - tire[r].localBound.extents.z &&
                    if (tire[r].position == tire[w].position && conatins && r != w)
                    {
                        tire[r].tireGroup = tireCount;
                        tire[w].tireGroup = tireCount;
                        System.Array.Resize <GameObject>(ref tireContainer, tireCount);
                    }
                    else if (tire[r].tireGroup == 0 && w == tire.Length - 1)
                    {
                        tire[r].tireGroup = tireCount;
                        System.Array.Resize <GameObject>(ref tireContainer, tireCount);
                    }
                }
                tireCount++;
            }
            tireCount--;

            WheelCollider[] tempWheels = createAditionalWheels(tireCount);
            findWheelsCenter();
            int         wheelCount = 1;
            TSTrafficAI trafficAI  = rigCarEmpty.GetComponent <TSTrafficAI>();
            TSSimpleCar simpleCar  = rigCarEmpty.GetComponent <TSSimpleCar>();
            trafficAI.frontWheels = new Transform[0];
            JointSpring spring = new JointSpring();
            spring.spring = 25000;
            spring.damper = 250;
            foreach (WheelCollider wheel in tempWheels)
            {
                wheel.suspensionSpring   = spring;
                wheel.suspensionDistance = 0.2f;
                wheel.mass = 5f;
                for (int r = 0; r < tire.Length; r++)
                {
                    if (tire[r].tireGroup == wheelCount)
                    {
                        if (wheel.transform.localPosition == Vector3.zero)
                        {
                            wheel.transform.localPosition = tire[r].tiresCenter;
                        }
                        wheel.name   = "Wheel" + tire[r].position + wheelCount;
                        wheel.radius = tire[r].wRadius;
                        if (tire[r].position == "FR")
                        {
                            wheel.GetComponentInChildren <TSSimpleCar_Wheel>().WheelPosition = "FR";
                        }
                        if (tire[r].position == "FL")
                        {
                            wheel.GetComponentInChildren <TSSimpleCar_Wheel>().WheelPosition = "FL";
                        }
                        if (tire[r].position == "RL")
                        {
                            wheel.GetComponentInChildren <TSSimpleCar_Wheel>().WheelPosition = "RL";
                        }
                        if (tire[r].position == "RR")
                        {
                            wheel.GetComponentInChildren <TSSimpleCar_Wheel>().WheelPosition = "RR";
                        }

                        if (tire[r].position == "FR" || tire[r].position == "FL")
                        {
                            simpleCar.FrontWheels = simpleCar.FrontWheels.Add(wheel);
                            System.Array.Resize <Transform>(ref trafficAI.frontWheels, trafficAI.frontWheels.Length + 1);
                            trafficAI.frontWheels[trafficAI.frontWheels.Length - 1] = wheel.transform;
                        }
                        if (tire[r].position == "RR" || tire[r].position == "RL")
                        {
                            simpleCar.BackWheels = simpleCar.BackWheels.Add(wheel);
                        }

                        foreach (Transform tireT in wheel.GetComponentsInChildren <Transform>())
                        {
                            if (tireT && tireT.name == "Tire")
                            {
                                tire[r].tireObj.transform.parent = tireT.transform;
                                wheel.radius *= tire[r].tireObj.transform.localScale.y;
                                if (tire[r].position == "FR" || tire[r].position == "FL")
                                {
                                    frontWRadius = wheel.radius;
                                }
                                if (tire[r].position == "RR" || tire[r].position == "RL")
                                {
                                    rearWRadius = wheel.radius;
                                }
                            }
                        }
                    }
                }
                wheelCount++;
            }
            addAntirollbars();
            Selection.activeObject = null;
        }
        else
        {
            guiOptions[2] = false;
            EditorUtility.DisplayDialog("Warning!", "Please select at least 3 GameObject which represent the model's Tyres!", "Ok");
        }
    }
コード例 #5
0
    public void OnGUI()
    {
        scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(position.width), GUILayout.Height(position.height));
        GUILayout.BeginHorizontal();
        GUILayout.Space(position.width / 2 - 75f);
        GUILayout.Label("Car Rig - Wizard", GUILayout.Width(150));
        GUILayout.EndHorizontal();
        GUILayout.Space(20);
        GUILayoutOption[] options = new GUILayoutOption[2];
        options[0] = GUILayout.Width(80);
        options[1] = GUILayout.Height(80);


        //First option for selecting the main doby of the car
        switch (menuStage)
        {
        case 0:

            prueba = EditorGUILayout.ObjectField(prueba, typeof(Object), true);
            if (prueba)
            {
                GUILayout.BeginHorizontal();
                if (GUILayout.Button(AssetPreview.GetAssetPreview((GameObject)prueba), options))
                {
                    if (center)
                    {
                        DestroyImmediate(center);
                    }
                    newBody = (GameObject)PrefabUtility.InstantiatePrefab(prueba);

#if UNITY_2018_3_OR_NEWER
                    PrefabUtility.UnpackPrefabInstance(newBody, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
#endif

                    if (rigCarEmpty.GetComponentsInChildren <Rigidbody>().Length == 0)
                    {
                        rigCarEmpty.AddComponent <Rigidbody>();
                    }
                    rigCarEmpty.AddComponent <AudioSource>();
                    rigCarEmpty.AddComponent <TSSimpleCar>();
                    rigCarEmpty.AddComponent <TSTrafficAI>();
                    newBody.transform.position    = Vector3.zero;
                    newBody.transform.eulerAngles = Vector3.zero;
                    carCenter(newBody.transform);
                    center.transform.parent        = rigCarEmpty.transform;
                    center.transform.localPosition = Vector3.zero;
                    center.transform.eulerAngles   = Vector3.zero;
                    size      = carSize();
                    menuStage = 1;
                }
                GUILayout.Label("<--- Click to continue");
                GUILayout.EndHorizontal();
                GUILayoutOption[] options1 = new GUILayoutOption[2];
                options1[0] = GUILayout.Width(300);
                options1[1] = GUILayout.Height(300);
                GUILayout.Label("Notes:  This rigger is intended for creating \nnew cars, you need to center the car on\nthe three axis (red, blue and green) using\nthe buttons included on this wizard. \nPlease do not manually adjust its\nposition or rotation.", options1);
            }

            break;

        case 1:

            GUILayout.BeginHorizontal();
            GUILayout.Label("Change Position of Car", GUILayout.Width(245));
            GUILayout.EndHorizontal();
            GUILayout.Space(15);
            changePosition(center.transform);

            // Rotation
            GUILayout.Space(25);
            GUILayout.BeginHorizontal();
            GUILayout.Label("Change Rotation of Car", GUILayout.Width(245));
            GUILayout.EndHorizontal();
            GUILayout.Space(15);
            changeRotation(center.transform);

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Back", GUILayout.Width(50)))
            {
                menuStage = 0;
            }
            GUILayout.Space(15);
            if (GUILayout.Button("Ok", GUILayout.Width(50)))
            {
                menuStage = 2;
            }
            GUILayout.EndHorizontal();

            break;

        case 2:

            GUILayout.Space(20);
            GUILayout.BeginHorizontal();
            GUILayout.Label("Adding Car Wheels \n\n" +
                            "Please select all your Tyre GameObjects \n" +
                            "Exclude the Calippers if the model \nhas them \n\n" +
                            "Press Ok when finished", GUILayout.Width(350));
            GUILayout.EndHorizontal();
            GUILayout.Space(20);

            GUILayout.BeginHorizontal();

            if (GUILayout.Button("Back", GUILayout.Width(50)))
            {
                menuStage = 1;
            }

            GUILayout.Space(15);

            if (GUILayout.Button("Ok", GUILayout.Width(50)))
            {
                assignWheels();
                SceneView.RepaintAll();
                menuStage = 3;
            }
            GUILayout.EndHorizontal();

            break;

        case 3:

            GUI.changed = false;
            activateWheelGizmos();
            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            GUILayout.Label("Verify the model's wheels Size", GUILayout.Width(350));
            GUILayout.EndHorizontal();
            GUILayout.Space(15);

            GUILayout.BeginHorizontal();
            GUILayout.Label("Front Wheels Radius: ", GUILayout.Width(245));
            options[0] = GUILayout.Width(35);
            options[1] = GUILayout.Height(20);
            GUILayout.EndHorizontal();
            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("+", options))
            {
                frontWRadius += wheelRStep;
                changeWheelsRadius(0, frontWRadius);
            }

            GUILayout.Space(15);
            if (GUILayout.Button("-", options))
            {
                frontWRadius -= wheelRStep;
                changeWheelsRadius(0, frontWRadius);
            }
            GUILayout.EndHorizontal();


            GUILayout.Space(15);

            GUILayout.BeginHorizontal();
            GUILayout.Label("Aplly Front Wheels Radius to all Wheels? ", GUILayout.Width(245));
            GUILayout.EndHorizontal();
            GUILayout.Space(10);

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Apply", GUILayout.Width(50)))
            {
                changeWheelsRadius(2, frontWRadius);
            }

            GUILayout.EndHorizontal();


            GUILayout.Space(15);

            GUILayout.BeginHorizontal();
            GUILayout.Label("Rear Wheels Radius: ", GUILayout.Width(245));

            GUILayout.EndHorizontal();
            GUILayout.Space(10);


            GUILayout.BeginHorizontal();

            if (GUILayout.Button("+", options))
            {
                rearWRadius += wheelRStep;
                changeWheelsRadius(1, rearWRadius);
            }

            GUILayout.Space(15);

            if (GUILayout.Button("-", options))
            {
                rearWRadius -= wheelRStep;
                changeWheelsRadius(1, rearWRadius);
            }
            GUILayout.EndHorizontal();

            GUILayout.Space(20);
            GUILayout.BeginHorizontal();
            GUILayout.Label("Aplly Rear Wheels Radius to all Wheels? ", GUILayout.Width(245));

            GUILayout.EndHorizontal();


            GUILayout.Space(10);


            GUILayout.BeginHorizontal();

            if (GUILayout.Button("Apply", GUILayout.Width(50)))
            {
                changeWheelsRadius(2, rearWRadius);
            }

            GUILayout.EndHorizontal();


            GUILayout.Space(15);
            GUILayout.BeginHorizontal();
            //				wheelRStep = EditorGUILayout.FloatField("Radius Step: ",Mathf.Clamp(wheelRStep,0f,1f));
            wheelRStep = EditorGUILayout.Slider("Radius Step:", wheelRStep, 0f, 1f);
            GUILayout.EndHorizontal();


            GUILayout.BeginHorizontal();
            GUILayout.TextArea("Standard wheel radius for selection in centimeters");
            GUILayout.EndHorizontal();

            GUILayout.Space(10);

            GUILayout.BeginHorizontal();
            GUILayout.Space(position.width / 2 - 85);
            if (GUILayout.Button("30", GUILayout.Width(50)))
            {
                rearWRadius  = 0.3f;
                frontWRadius = 0.3f;
                changeWheelsRadius(2, rearWRadius);
            }

            GUILayout.Space(10);
            if (GUILayout.Button("40", GUILayout.Width(50)))
            {
                rearWRadius  = 0.4f;
                frontWRadius = 0.4f;
                changeWheelsRadius(2, rearWRadius);
            }

            GUILayout.Space(10);
            if (GUILayout.Button("25", GUILayout.Width(50)))
            {
                rearWRadius  = 0.25f;
                frontWRadius = 0.25f;
                changeWheelsRadius(2, rearWRadius);
            }

            GUILayout.EndHorizontal();
            GUILayout.Space(20);

            drivetrainSelected = true;

            GUILayout.BeginHorizontal();
            GUILayout.Space(position.width / 2 - 75);
            if (GUILayout.Button("Back", GUILayout.Width(50)))
            {
                menuStage          = 2;
                drivetrainSelected = false;
            }

            GUILayout.Space(20);

            if (GUILayout.Button("Continue", GUILayout.Width(80)))
            {
                if (drivetrainSelected || isTrailer)
                {
                    deactivateWheelGizmos();

                    menuStage = 4;
                }
                else
                {
                    EditorUtility.DisplayDialog("Warning!", "Pleas select the desired Drivetrain for this car", "Ok");
                }
            }
            GUILayout.EndHorizontal();
            break;

        case 4:

            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            GUILayout.TextArea("Select the GameObjects " +
                               "to add a MeshCollider or " +
                               "BoxCollider to, this would " +
                               "be the colliders of the car");
            GUILayout.EndHorizontal();
            GUILayout.Space(15);

            GUILayout.BeginHorizontal();

            convex = EditorGUILayout.Toggle("Convex?", convex);

            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();

            if (GUILayout.Button("Add MeshCollider", GUILayout.Width(200)))
            {
                if (Selection.activeObject != null)
                {
                    foreach (GameObject tempObj in Selection.gameObjects)
                    {
                        tempObj.AddComponent <MeshCollider>();
                        if (convex)
                        {
                            tempObj.GetComponent <MeshCollider>().convex = true;
                        }
                    }
                }
            }

            GUILayout.EndHorizontal();

            GUILayout.Space(20);

            GUILayout.BeginHorizontal();

            if (GUILayout.Button("Add BoxCollider", GUILayout.Width(200)))
            {
                if (Selection.activeObject != null)
                {
                    foreach (GameObject tempObj in Selection.gameObjects)
                    {
                        tempObj.AddComponent <BoxCollider>();
                    }
                }
            }

            GUILayout.EndHorizontal();

            GUILayout.Space(20);


            GUILayout.BeginHorizontal();
            GUILayout.Space(position.width / 2 - 75);
            if (GUILayout.Button("Back", GUILayout.Width(50)))
            {
                menuStage = 3;
            }

            GUILayout.Space(20);

            if (GUILayout.Button("Continue", GUILayout.Width(80)))
            {
                if (rigCarEmpty.GetComponentInChildren <MeshCollider>() || rigCarEmpty.GetComponentInChildren <BoxCollider>())
                {
                    menuStage = 5;
                }
                else
                {
                    EditorUtility.DisplayDialog("Warning!", "Pleas add Colliders for this car", "Ok");
                }
            }
            GUILayout.EndHorizontal();

            break;

        case 5:

            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            GUILayout.TextArea("Select the GameObjects that have the brakelights " +
                               "and select from the dopdown list " +
                               "the material used for the brakelights" +
                               " The shader of this material " +
                               "would be changed to LightEmmisive");
            GUILayout.EndHorizontal();
            GUILayout.Space(15);

            if (Selection.activeObject != null)
            {
                //					GameObject[] selectedObjects = Selection.gameObjects;
                if (Selection.activeGameObject.GetComponent <Renderer>())
                {
                    materialsSelected      = Selection.activeGameObject.GetComponent <Renderer>().sharedMaterials;
                    materialsSelectedNames = new string[materialsSelected.Length];
                    int i = 0;
                    foreach (Material material in materialsSelected)
                    {
                        materialsSelectedNames[i] = material.name;
                        i++;
                    }
                    materialSelected = EditorGUILayout.Popup(materialSelected, materialsSelectedNames);
                }
            }

            GUILayout.Space(15);


            GUILayout.BeginHorizontal();
            GUILayout.Space(position.width / 2 - 75);
            if (GUILayout.Button("Back", GUILayout.Width(50)))
            {
                menuStage = 4;
            }

            GUILayout.Space(20);

            if (GUILayout.Button("Continue", GUILayout.Width(80)))
            {
                if (Selection.activeObject)
                {
                    menuStage = 6;
                    TSSimpleCar simpleCar        = rigCarEmpty.GetComponent <TSSimpleCar>();
                    int         totalbrakeLights = 0;
                    foreach (Transform t in Selection.transforms)
                    {
                        totalbrakeLights += t.GetComponentsInChildren <Renderer>().Length;
                    }
                    simpleCar.brakeLigths = new Renderer[totalbrakeLights];
                    int brakeConuter = 0;
                    foreach (Transform t in Selection.transforms)
                    {
                        foreach (Renderer r in t.GetComponentsInChildren <Renderer>())
                        {
                            simpleCar.brakeLigths[brakeConuter] = r;
                            brakeConuter++;
                        }
                    }
                    simpleCar.enableDisableBrakeLights = true;
                }
                else
                {
                    EditorUtility.DisplayDialog("Warning!", "Pleas select the desired BrakeLights for this car", "Ok");
                }
            }
            GUILayout.EndHorizontal();

            break;

        case 6:

            GUILayout.BeginHorizontal();
            GUILayout.TextArea("Give the car a name to save it as a prefab \n\n" +
                               "Note: The prefab would be saved under the Resources/Cars Folder");
            GUILayout.EndHorizontal();
            GUILayout.Space(15);
            GUILayout.BeginHorizontal();
            rigCarEmpty.name = EditorGUILayout.TextField("Car Name: ", rigCarEmpty.name);

            GUILayout.EndHorizontal();
            GUILayout.Space(15);

            GUILayout.BeginHorizontal();
            GUILayout.Space(position.width / 2 - 75);

            rigCarEmpty.GetComponent <Rigidbody>().mass = EditorGUILayout.FloatField("Car Mass: ", rigCarEmpty.GetComponent <Rigidbody>().mass);

            if (GUILayout.Button("Save & Finish", GUILayout.Width(150)))
            {
                GameObject CoG = new GameObject("CoG");
                CoG.transform.parent   = rigCarEmpty.transform;
                CoG.transform.position = new Vector3(0, 0.2f, 0);
                TSSimpleCar simpleCar = rigCarEmpty.GetComponent <TSSimpleCar>();
                simpleCar.CoM                = CoG.transform;
                simpleCar.EngineTorque       = 80;
                simpleCar.brakeTorque        = 150;
                simpleCar.maxAcceleration    = 5;
                simpleCar.superSimplePhysics = false;
                BoxCollider box = rigCarEmpty.AddComponent <BoxCollider>();
                box.isTrigger = true;
                TSTrafficAI trafficAI = rigCarEmpty.GetComponent <TSTrafficAI>();
                trafficAI.playerSensor = box;
                rigCarEmpty.layer      = LayerMask.NameToLayer("Traffic AI");
                rigCarEmpty.tag        = "TrafficCar";
                CreatePrefab();

                this.Close();
            }
            GUILayout.EndHorizontal();

            break;
        }
        EditorGUILayout.EndScrollView();
    }
コード例 #6
0
ファイル: TSSimpleCar.cs プロジェクト: dhanil/driVRRepo
    /// <summary>
    /// All wheels.
    /// </summary>
    /*PART OF EXPERIMENTAL CODE TO AUTO SWITCH FROM AND TO SUPER SIMPLE PHYSICS*/
    //    private TSSimpleCar_Wheel[] allWheels;
    void Awake()
    {
        //Get the cached references
        myTransform = transform;
        body = GetComponent<Rigidbody>();
        bodies = GetComponentsInChildren<Rigidbody>();
        trafficAI = GetComponent<TSTrafficAI>();
        audioSource = GetComponent<AudioSource>();
        if (audioSource ==null)engineAudioEnabled = false;

        /*PART OF EXPERIMENTAL CODE TO AUTO SWITCH FROM AND TO SUPER SIMPLE PHYSICS*/
        //		allWheels = GetComponentsInChildren<TSSimpleCar_Wheel>();
    }