using System.Collections; using System.Collections.Generic; using Unity.MLAgents; using UnityEngine; public class ObstaclePatternBehaviour : MonoBehaviour { [SerializeField] private GameHandler gameHandler; private float gameObjectLength; private float gameSpeed; private GameHandler _gameHandler; // Start is called before the first frame update void Awake() { _gameHandler = GameHandler.Instance; } private void Start() { gameObjectLength = ComputeLengthRecursive(0.0f, gameObject.transform) - gameObject.transform.position.x; } // Update is called once per frame void Update() { // move the obstacle depending on GameHandler's offset value gameObject.transform.position += new Vector3(-_gameHandler.FrameDistance, 0, 0); //kill the obstacle if it reach the GameHandler's dead end value var position = gameObject.transform.position.x; var spawnerPosition = LevelSpawner.Instance.transform.position.x; //Debug.Log("position : " + position+"\n"+"scale : " + gameObjectLength); if(position < -gameObjectLength - spawnerPosition) { //Debug.Log("DESTROY"); MonoBehaviour.Destroy(gameObject); return; } } private float ComputeLengthRecursive(float max, Transform @object) { foreach (Transform child in @object) { if(child.position.x > max) { max = child.position.x; } max = ComputeLengthRecursive(max, child); } return max; } //private float ComputeLength() //{ // Debug.Log("ComputeLength"); // CenterAndChildCount centerAndChildCount = new(); // centerAndChildCount = ComputeCenterRecursive(centerAndChildCount, gameObject.transform); // Vector3 center = centerAndChildCount.center; // int childCount = centerAndChildCount.childCount; // center /= childCount; //center is average center of children // Bounds bounds = new Bounds(center, Vector3.zero); // bounds = ComputeBoundsRecursive(bounds, gameObject.transform); // return bounds.size.x; //} //private CenterAndChildCount ComputeCenterRecursive(CenterAndChildCount centerAndChildCount, Transform @object) //{ // foreach (Transform child in @object) // { // centerAndChildCount.center += child.gameObject.GetComponent().bounds.center; // centerAndChildCount.childCount++; // centerAndChildCount = ComputeCenterRecursive(centerAndChildCount, child); // } // return centerAndChildCount; //} //private Bounds ComputeBoundsRecursive(Bounds bounds, Transform @object) //{ // foreach (Transform child in @object) // { // ComputeBoundsRecursive(bounds, child); // bounds.Encapsulate(child.gameObject.GetComponent().bounds); // } // return bounds; //} //private class CenterAndChildCount //{ // public Vector3 center = Vector3.zero; // public int childCount = 0; //} }