/// <summary> /// Spawns an object at the specified position and determines the next spawn position /// </summary> /// <param name="spawnPosition">Spawn position.</param> protected virtual void LinkedSpawn(Vector3 spawnPosition) { GameObject spawnedObject = Spawn(spawnPosition); if (spawnedObject == null) { if (_lastSpawnedTransform == null) { _lastSpawnedTransform = this.transform; } _nextSpawnDistance = 0f; } else { LinkedSpawnedObject spawnedLinkedSpawnedObject = spawnedObject.GetComponent <LinkedSpawnedObject>(); if (_lastLinkedSpawnedObject != null) { // we reposition the linked spawned object to have its In match the previously spawned object's Out. //Vector3 newPosition = spawnedObject.In; spawnedObject.transform.position = _lastSpawnedTransform.position + _lastLinkedSpawnedObject.Out - spawnedLinkedSpawnedObject.In; } _lastSpawnedTransform = spawnedObject.transform; _lastLinkedSpawnedObject = spawnedLinkedSpawnedObject; // we define the next spawn position based on the size of the current object and the specified gaps _nextSpawnDistance = 0f; } }
public virtual void OnEnable() { _linkedSpawnedObject = (LinkedSpawnedObject)target; _renderer = _linkedSpawnedObject.GetComponent <Renderer>(); _style = new GUIStyle(); _style.normal.textColor = Color.green; if (_linkedSpawnedObject.In == Vector3.zero && _linkedSpawnedObject.Out == Vector3.zero) { ResetInOutPosition(); } }
public override void OnInspectorGUI() { _linkedSpawnedObject = (LinkedSpawnedObject)target; EditorGUILayout.LabelField("In/Out Set up", _linkedSpawnedObject.InOutSetup.ToString()); DrawDefaultInspector(); if (GUILayout.Button("In/Out Reset")) { ResetInOutPosition(); } }
/// <summary> /// Triggered every frame /// </summary> protected virtual void FixedUpdate() { if (OnlySpawnWhileGameInProgress) { if (GameManager.Instance.Status != GameManager.GameStatus.GameInProgress) { _lastSpawnedTransform = null; _lastLinkedSpawnedObject = null; return; } } if ((_lastSpawnedTransform == null) || (!_lastSpawnedTransform.gameObject.activeInHierarchy)) { FirstSpawn(); } if (_lastSpawnedTransform != null) { /// if we've reached the next spawn position, we spawn a new object if (transform.position.x - _lastSpawnedTransform.position.x >= _nextSpawnDistance) { /// we reposition the object Vector3 spawnPosition; if (_lastSpawnedTransform != this.transform) { spawnPosition = _lastSpawnedTransform.transform.position + _lastSpawnedTransform.GetComponent <LinkedSpawnedObject>().Out; } else { spawnPosition = _lastSpawnedTransform.transform.position; } LinkedSpawn(spawnPosition); } } }