示例#1
0
    public void GenerateNewPath()
    {
        if (instantiatedGoal != null)
        {
            Destroy(instantiatedGoal);
        }

        if (checkpoints.Count > 0)
        {
            foreach (GameObject checkpoint in checkpoints)
            {
                Destroy(checkpoint);
            }
        }

        checkpoints = new List <GameObject>();

        pathInfo.GenerateNewPath();

        float newZ = 0.0f;

        float randomizeOffsetX = Random.value < 0.5f ? 1.0f : -1.0f;

        for (int i = 0; i < pathInfo.GetPathLength(); i++)
        {
            float newX = 0.0f;
            float newY = 0.0f;

            if (pathInfo.GetHorizontal())
            {
                if (i % 2 == 0)
                {
                    newX = Random.Range(2.0f, pathInfo.GetHorizontalDistance()) * randomizeOffsetX;
                }
                else
                {
                    newX = Random.Range(-pathInfo.GetHorizontalDistance(), -2.0f) * randomizeOffsetX;
                }
            }

            if (pathInfo.GetVertical())
            {
                newY = Random.Range(-pathInfo.GetVerticalDistance(), pathInfo.GetVerticalDistance());
            }

            pathInfo.SetPathNode(i, new Vector3(newX, newY, newZ));
            newZ += pathInfo.GetRandomNodeDistance();
        }

        pathCreator.bezierPath         = new BezierPath(transform.position);
        pathCreator.bezierPath         = GeneratePath(pathInfo.GetPath());
        roadMeshCreator.flattenSurface = true;
        roadMeshCreator.roadWidth      = pathInfo.GetRandomRoadWidth();
        // for(int i = 0; i < roadRoot.transform.childCount; i++)
        // {
        //     Destroy(roadRoot.transform.GetChild(i).gameObject);
        // }
        roadMeshCreator.TriggerUpdate();
        instantiatedGoal = (GameObject)Instantiate(goalPrefab);
        instantiatedGoal.transform.SetParent(transform, false);
        instantiatedGoal.GetComponent <BoxCollider>().size = new Vector3(roadMeshCreator.roadWidth + 5.0f, 4.0f, 5.0f);
        instantiatedGoal.transform.localRotation           = pathCreator.path.GetRotationAtDistance(pathCreator.path.length);
        instantiatedGoal.transform.Rotate(0.0f, 0.0f, 90.0f);
        int furthestPoint = pathCreator.bezierPath.NumPoints - 1;

        instantiatedGoal.transform.localPosition = pathCreator.bezierPath.GetPoint(furthestPoint);
        carAgent.transform.localPosition         = pathCreator.bezierPath.GetPoint(0);
        carAgent.transform.localRotation         = pathCreator.path.GetRotation(0);
        carAgent.transform.Rotate(0.0f, 0.0f, 90.0f);
        carAgent.transform.position            += transform.forward * 3.0f;
        carAgent.GetComponent <CarAgent>().goal = instantiatedGoal;
        // carAgent.transform.position += transform.TransformDirection(Vector3.up);
        carAgent.GetComponent <Rigidbody>().isKinematic            = false;
        carAgent.GetComponent <Rigidbody>().collisionDetectionMode = CollisionDetectionMode.Continuous;
        roadRoot.transform.GetChild(0).GetComponent <MeshRenderer>().material.mainTextureScale = new Vector2(1.0f, (float)pathInfo.GetPathLength() * 2.0f);
        roadRoot.transform.GetChild(0).gameObject.GetComponent <MeshFilter>().mesh.RecalculateNormals();
        for (int i = 0; i < roadRoot.transform.GetChild(0).gameObject.GetComponents <MeshCollider>().Length; i++)
        {
            Destroy(roadRoot.transform.GetChild(0).gameObject.GetComponents <MeshCollider>()[0]);
        }

        roadRoot.transform.GetChild(0).gameObject.AddComponent <MeshCollider>();
        roadRoot.transform.GetChild(0).gameObject.GetComponent <MeshCollider>().sharedMesh =
            roadRoot.transform.GetChild(0).GetComponent <MeshFilter>().sharedMesh;

        // roadRoot.transform.GetChild(0).gameObject.GetComponent<MeshCollider>().sharedMesh = roadRoot.transform.GetChild(0).gameObject.GetComponent<MeshFilter>().sharedMesh;
        // transform.GetChild(2).GetComponent<MeshCollider>().convex = true;

        float dst       = 0.0f;
        float increment = 25.0f;

        while (dst < (pathCreator.path.length - increment))
        {
            if (dst < increment)
            {
                dst += increment;
                continue;
            }

            GameObject checkpoint = (GameObject)Instantiate(checkpointPrefab);
            checkpoint.transform.SetParent(transform, false);
            checkpoint.transform.position      = pathCreator.path.GetPointAtDistance(dst);
            checkpoint.transform.localRotation = pathCreator.path.GetRotationAtDistance(dst);
            checkpoint.transform.Rotate(0.0f, 0.0f, 90.0f);
            checkpoints.Add(checkpoint);
            dst += increment;
        }
    }
示例#2
0
    private void generatePath()
    {
        bounds = trackArea.GetComponent <MeshRenderer>().bounds;
        List <Vector3> points     = new List <Vector3>();
        int            num_points = Random.Range(Points_min, Points_max);


        RoadMeshCreator pst = (RoadMeshCreator)GetComponent <PathSceneTool>();
        PathCreator     pc  = GetComponent <PathCreator>();

        for (int i = 0; i < num_points; i++)
        {
            float size_x = bounds.size.x;
            float size_z = bounds.size.z;
            float size_y = bounds.size.y;

            //print(string.Format("Size X: {0}, Y: {1} Z: {2}", size_x, size_y, size_z));

            float x = Random.Range(bounds.center.x - size_x / 2, bounds.center.x + size_x / 2);
            float z = Random.Range(bounds.center.z - size_z / 2, bounds.center.z + size_z / 2);
            float y = 1;

            // I want to use some dumb raycasting so that the Y level always is on top of the terrain
            RaycastHit hit;
            if (Physics.Raycast(new Vector3(x, 10000, z), Vector3.down, out hit))
            {
                y = hit.point.y + pst.thickness;
            }

            //print(string.Format("Point X: {0}, Y: {1} Z: {2}", x, y, z));

            Vector3 point = new Vector3(x, y, z);
            points.Add(point);
        }

        trackPath = new BezierPath(points, true, PathSpace.xyz);
        trackPath.AutoControlLength = Random.Range(0.1f, 1.0f);
        trackPath.IsClosed          = true;

        /*
         * for(int i = 0; i < trackPath.NumPoints; i++)
         * {
         *  Vector3 point = trackPath.GetPoint(i);
         *  float y = Random.Range(0.0f, 10.0f);
         *  point.y = y;
         * }
         */
        trackPath.NotifyPathModified();

        pc.bezierPath = trackPath;

        pst.TriggerUpdate();

        GameObject   road         = GameObject.Find("Road Mesh Holder");
        MeshCollider roadCollider = road.GetComponent <MeshCollider>();

        Destroy(roadCollider);
        roadCollider = road.AddComponent <MeshCollider>();
        MeshFilter roadMesh = road.GetComponent <MeshFilter>();

        roadCollider.sharedMesh = roadMesh.mesh;
    }