Hi Guys,
I have created two classes Road and StreetGenerator.
Road makes road (list) from lanes with specific width. StreetGenerator supposed to make left side of road and right side of the road using MakeRoad() method from Road class.
Questions:
-
Where would you keep all the variable for road creation? (in Road or StreetGenerator)
-
I managed to reference Road class in StreetGenerator, but how I create two roads? If I use MakeRoad() twice I still got just one road.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Road : MonoBehaviour
{
//Road control variables
public bool isRoadOnBothSides;
public GameObject lanePrefab;
public float laneWidth;
public int numberOfLanes;
private List laneList = new List();
private float laneOffset;// Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { // MakeRoad(); } //Create Road form lane objects. public List<GameObject> MakeRoad() { if (laneList.Count < numberOfLanes) { AddLane(); } else if (laneList.Count > numberOfLanes) { RemoveLane(); } ScaleLane(); //scales lane gameobjects in run time CheckLanePosition(); // checks and adjust lane position after scale change return laneList; } //Add lane public void AddLane() { laneOffset = laneWidth * laneList.Count; GameObject lane = Instantiate(lanePrefab, new Vector3(laneOffset, 0, 0), Quaternion.identity); laneList.Add(lane); } //Scale lane public void ScaleLane() { for (int i = 0; i < laneList.Count; i++) { laneList*.transform.localScale = new Vector3(laneWidth, 1, 1);*
//laneList*.GetComponent().size = new Vector2(laneWidth, 2.1f);*
}
}
//Check lane position
public void CheckLanePosition()
{
for (int i = 0; i < laneList.Count; i++)
{
laneList_.transform.position = new Vector3(laneWidth * i, 0, 0);
}
}_
//Remove lane
public void RemoveLane()
{
GameObject destroyLane = laneList[laneList.Count - 1];
laneList.Remove(destroyLane);
Destroy(destroyLane);
}
}
Am I doing something wrong with instantiate ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StreetGenerator : MonoBehaviour
{
Road road; // reference to Road class
private List leftRoad = new List(); // List of lanes on left side. This is a default list if isRoadOnBothSides is False
private List rightRoad = new List();
// Start is called before the first frame update
void Start()
{
road = FindObjectOfType(); // find Road class in the scene
}
// Update is called once per frame
void Update()
{
//leftRoad = road.MakeRoad();
//rightRoad = road.MakeRoad();
}
}