Skip to content

Instantly share code, notes, and snippets.

@r3ne-pew
Last active January 23, 2017 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r3ne-pew/12bb6ef49c5a298f21e8869ac6ffb857 to your computer and use it in GitHub Desktop.
Save r3ne-pew/12bb6ef49c5a298f21e8869ac6ffb857 to your computer and use it in GitHub Desktop.
Unity Traffic Spawner
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