Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class LaneController : MonoBehaviour {
- public bool traffic = true;
- public bool positiveFlow = true;
- public float speed = 1.0f;
- public float density = 0.5f;
- public Material pavementMaterial;
- public Material roadMaterial;
- public GameObject carPrefab;
- public bool canSpawnCar = true;
- // Use this for initialization
- void Start () {
- if (traffic)
- {
- GetComponent<MeshRenderer>().material = roadMaterial;
- } else
- {
- GetComponent<MeshRenderer>().material = pavementMaterial;
- }
- }
- // Update is called once per frame
- void Update () {
- if (canSpawnCar && traffic)
- {
- if(Random.Range(0.0f, 1.0f) < density)
- {
- canSpawnCar = false;
- GameObject go = (GameObject)Instantiate(carPrefab);
- Transform t = GetComponent<Transform>();
- Vector3 pos = t.position;
- pos.x = -0.5f * t.localScale.x;
- pos.y = 0.9f;
- if (!positiveFlow)
- {
- pos.x = pos.x * -1;
- Vector3 a = new Vector3(0, 180, 0);
- go.transform.localEulerAngles = a;
- }
- go.transform.position = pos;
- }
- }
- }
- public void enterEndZone(GameObject car, bool positiveEndZone)
- {
- if (positiveFlow && positiveEndZone)
- {
- Destroy(car);
- }
- else if (!positiveFlow && !positiveEndZone)
- {
- Destroy(car);
- }
- }
- public void exitEndZone(GameObject car, bool positiveEndZone)
- {
- if (positiveFlow && !positiveEndZone)
- {
- canSpawnCar = true;
- }
- else if (!positiveFlow && positiveEndZone)
- {
- canSpawnCar = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement