Skip to content

Instantly share code, notes, and snippets.

@Mazday21
Created August 13, 2022 15:58
Show Gist options
  • Save Mazday21/e503015d6e97739bee0b24499770f651 to your computer and use it in GitHub Desktop.
Save Mazday21/e503015d6e97739bee0b24499770f651 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class RoadSpawner : MonoBehaviour
{
[SerializeField] private List<GameObject> _roads;
[SerializeField] private Player _player;
private float _roadLenght = 89.6f;
private void OnEnable()
{
_player.TriggerRoadEntered += LayRoad;
}
private void OnDisable()
{
_player.TriggerRoadEntered -= LayRoad;
}
private void Start()
{
_roads = _roads.OrderBy(r => r.transform.position.z).ToList();
}
private void LayRoad()
{
GameObject movedRoad = _roads[0];
_roads.Remove(movedRoad);
float newPositionZ = _roads[_roads.Count - 1].transform.position.z + _roadLenght;
movedRoad.transform.position = new Vector3(0, 0, newPositionZ);
_roads.Add(movedRoad);
Debug.Log("LayRoad");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment