Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class LaneController : MonoBehaviour {
  6.  
  7. public bool traffic = true;
  8. public bool positiveFlow = true;
  9. public float speed = 1.0f;
  10. public float density = 0.5f;
  11.  
  12. public Material pavementMaterial;
  13. public Material roadMaterial;
  14.  
  15. public GameObject carPrefab;
  16.  
  17. public bool canSpawnCar = true;
  18.  
  19.  
  20. // Use this for initialization
  21. void Start () {
  22. if (traffic)
  23. {
  24. GetComponent<MeshRenderer>().material = roadMaterial;
  25. } else
  26. {
  27. GetComponent<MeshRenderer>().material = pavementMaterial;
  28. }
  29. }
  30.  
  31. // Update is called once per frame
  32. void Update () {
  33. if (canSpawnCar && traffic)
  34. {
  35. if(Random.Range(0.0f, 1.0f) < density)
  36. {
  37. canSpawnCar = false;
  38. GameObject go = (GameObject)Instantiate(carPrefab);
  39. Transform t = GetComponent<Transform>();
  40. Vector3 pos = t.position;
  41. pos.x = -0.5f * t.localScale.x;
  42. pos.y = 0.9f;
  43. if (!positiveFlow)
  44. {
  45. pos.x = pos.x * -1;
  46. Vector3 a = new Vector3(0, 180, 0);
  47. go.transform.localEulerAngles = a;
  48.  
  49. }
  50. go.transform.position = pos;
  51. }
  52. }
  53. }
  54.  
  55. public void enterEndZone(GameObject car, bool positiveEndZone)
  56. {
  57. if (positiveFlow && positiveEndZone)
  58. {
  59. Destroy(car);
  60. }
  61. else if (!positiveFlow && !positiveEndZone)
  62. {
  63. Destroy(car);
  64. }
  65. }
  66.  
  67. public void exitEndZone(GameObject car, bool positiveEndZone)
  68. {
  69. if (positiveFlow && !positiveEndZone)
  70. {
  71. canSpawnCar = true;
  72. }
  73. else if (!positiveFlow && positiveEndZone)
  74. {
  75. canSpawnCar = true;
  76. }
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement