Last active
January 23, 2017 09:44
-
-
Save r3ne-pew/12bb6ef49c5a298f21e8869ac6ffb857 to your computer and use it in GitHub Desktop.
Unity Traffic Spawner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class TrafficController : MonoBehaviour { | |
public GameObject Car; | |
public GameObject CarParent; | |
private bool spawnLeft; | |
private float[] lanes; | |
private float[] laneSpeeds; | |
private int carID; | |
// Use this for initialization | |
void Start () { | |
lanes = new float[] { -3.0f, -1.0f, 1.0f, 2.0f, 3.0f }; | |
laneSpeeds = new float[] { 8.0f, 5.0f, 4.0f, 3.0f, 2.0f }; | |
vehicleSpawner(); | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
void vehicleSpawner() | |
{ | |
GameObject newCar = Instantiate(Car); | |
newCar.name = "_car_ (" + carID+ ")"; | |
newCar.transform.parent = CarParent.transform; | |
SingleCarController scc = newCar.GetComponent<SingleCarController>(); | |
scc.startX = -7.6f; | |
scc.targetX = scc.startX * -1.0f; | |
int chosenLane = (int)Random.Range(0, 5); | |
scc.startZ = lanes[chosenLane]; | |
scc.speedMultiplikator = laneSpeeds[chosenLane]; | |
carID += 1; | |
Invoke("vehicleSpawner", Random.Range(0.4f, 0.9f)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment