Beispiel #1
0
    void Start()
    {
        start_time = Time.time;

        my_cars = new List <GameObject>();
        planner1 planner = GameObject.Find("GraphObj").GetComponent <planner1>();

        race_car.transform.position = terrain_manager.myInfo.start_pos + 2f * Vector3.up;
        race_car.transform.rotation = Quaternion.identity;

        for (int i = 0; i < no_of_cars; i++)
        {
            GameObject new_car;
            UnityStandardAssets.Vehicles.Car.CarAI new_AI;

            new_car = Instantiate(race_car, new Vector3(20.0f + i * 8.0f, 10.0f, 20f), Quaternion.identity);
            Vector3 nominal_pos = CircularConfiguration(i + (int)Mathf.Floor(no_of_cars / 2), no_of_cars, 0.75f);
            new_car.transform.position = GetCollisionFreePosNear(nominal_pos, 50f);
            my_cars.Add(new_car);

            GameObject car_sphere = new_car.transform.Find("Sphere").gameObject;
            Color      my_color   = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
            car_sphere.GetComponent <Renderer>().material.SetColor("_Color", my_color);

            GameObject goal_sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            goal_sphere.layer = 11;
            Collider c = goal_sphere.GetComponent <Collider> ();
            c.enabled = false;

            nominal_pos = CircularConfiguration(i, no_of_cars, 0.8f);
            goal_sphere.transform.position = GetCollisionFreePosNear(nominal_pos, 50f);

            goal_sphere.transform.localScale = Vector3.one * 3f;
            goal_sphere.GetComponent <Renderer>().material.SetColor("_Color", my_color);

            // Assign goal point
            new_AI = new_car.GetComponent <UnityStandardAssets.Vehicles.Car.CarAI>();
            new_AI.my_goal_object = goal_sphere;
            new_AI.terrain_manager_game_object = terrain_manager_game_object;
            new_AI.Aggressiveness = no_of_cars - i;
            new_AI.delay          = 3 * no_of_cars * ((int)(i)); // Random.Range(0, no_of_cars*no_of_cars)*
            new_AI.my_color       = my_color;

            new_AI.ownPath = planner.plan(new_car.transform.position, goal_sphere.transform.position, my_color);

            //new_AI.my_goal = goal_sphere.transform.position;
            //var cubeRenderer = new_sphere.GetComponent<Renderer>();
            //var cubeRenderer = new_car.GetComponent<Sphere>();


            //Call SetColor using the shader property name "_Color" and setting the color to red
            //cubeRenderer.material.SetColor("_Color", Color.cyan);
        }
    }
Beispiel #2
0
        private void Start()
        {
            m_Car           = GetComponent <CarController>();
            terrain_manager = terrain_manager_game_object.GetComponent <TerrainManager>();

            Vector3 start_pos = transform.position; // terrain_manager.myInfo.start_pos;

            goal_pos = my_goal_object.transform.position;
            planner  = GameObject.Find("GraphObj").GetComponent <planner1>();
            ownPath  = planner.plan(start_pos, goal_pos, my_color);
            mapGraph = GameObject.Find("GraphObj").GetComponent <TravGraph>().mapGraph;


            friends = GameObject.FindGameObjectsWithTag("Player");

            oldAggressiveness = Aggressiveness;

            behaviorTree = new Root(
                new Selector(
                    new BlackboardCondition("NotOnGoal", Operator.IS_EQUAL, true, Stops.SELF,
                                            new Selector(
                                                new BlackboardCondition("Wall", Operator.IS_EQUAL, true, Stops.SELF,
                                                                        new Action(() => rePlan())
                                                                        ),

                                                new BlackboardCondition("Car", Operator.IS_EQUAL, true, Stops.SELF,
                                                                        new Sequence(
                                                                            new Action(() => calculateBearing()),
                                                                            new Action(() => Avoid())
                                                                            )
                                                                        ),

                                                new Action(() => followPath())

                                                )
                                            ),
                    new Action(() => goalAdjust())
                    )

                );
            blackboard = behaviorTree.Blackboard;


#if UNITY_EDITOR
            Debugger debugger = (Debugger)this.gameObject.AddComponent(typeof(Debugger));
            debugger.BehaviorTree = behaviorTree;
#endif

            //behaviorTree.Start();
        }