Example #1
0
    void Init_Setup()
    {
        List_Road = new GameObject[Road_Size, Road_Size];
        gameObject.transform.localScale = new Vector3(Road_Size * 27, 100, Road_Size * 27);
        roadCreater = new RoadCreater(Road_Size);

        roadCreater.Init_Road();
        for (int i = 0; i < Road_Size; i++)
        {
            for (int j = 0; j < Road_Size; j++)
            {
                if (roadCreater.Array_Road[i, j] != null && roadCreater.Array_Road[i, j].Is_Make_Prefab == false)
                {
                    Make_Prefab(roadCreater.Array_Road[i, j].Prefab_Num, i, j, -(27 * (int)(Road_Size / 2)) + (j * 27) + Start_X,
                                27 * (int)(Road_Size / 2) - (i * 27) + Start_Z);

                    roadCreater.Array_Road[i, j].Is_Make_Prefab = true;
                }
            }
        }
    }
    private void OnValidate()
    {
        // get road data
        if (waypoints == null || waypoints.Length <= 0)
        {
            RoadCreater roadCreater = road.GetComponent <RoadCreater>();
            Vector3[]   points      = road.GetComponent <PathCreater>().path.GetEvenlySpacedPoints(roadCreater.spacing);
            bool        isClosed    = road.GetComponent <PathCreater>().path.IsClosed;
            float       roadWidth   = roadCreater.roadWidth;
            // declare and init using variables
            int     index = 0;
            Vector3 forward;
            waypoints = new Vector3[points.Length * 2];
            float height = points[0].y;

            // go throw each road point to find relative points on right and left side
            for (int i = 0; i < points.Length; i++)
            {
                forward = Vector3.zero;
                if (i < points.Length - 1 || isClosed)
                {
                    forward += points[(i + 1) % points.Length] - points[i];
                }
                if (i > 0 || isClosed)
                {
                    forward += points[i] - points[(i - 1 + points.Length) % points.Length];
                }
                forward.y = height;
                forward.Normalize();
                Vector3 left = new Vector3(-forward.z, height, forward.x);
                waypoints[index]     = points[i] + left * (roadWidth + 3) * 0.5f;
                waypoints[index + 1] = points[i] - left * (roadWidth + 3) * 0.5f;
                index += 2;
            }
        }
    }
 private void OnEnable()
 {
     creater = (RoadCreater)target;
 }