public void DirectTo(Transform target) { //Clear the existing coroutines. if (orbitCoroutine != null) { StopCoroutine(orbitCoroutine); } if (directToCoroutine != null) { StopCoroutine(directToCoroutine); } //Set new location. targetDestination = target; //Determine how the ship will react upon arrival. if (targetDestination.gameObject.tag.Equals("Space Station")) { state = DirectableState.MovingToSpaceStation; } else { state = DirectableState.TrackingEnemy; } //Start the coroutine. directToCoroutine = DirectToCoroutine(); StartCoroutine(directToCoroutine); }
protected override void BeginToOrbit(Transform someSpaceStation) { //End the other coroutine. StopCoroutine(directToCoroutine); //Actually DO the thing. base.BeginToOrbit(someSpaceStation); //Set the state appropriately. state = DirectableState.OrbitingSpaceStation; }
private void FindAndMoveTowardNearestSpaceStation() { //Find the nearest space station. GameObject[] spaceStations = GameObject.FindGameObjectsWithTag("Space Station"); GameObject nearestSpaceStation = null; float leastDistance = float.MaxValue; foreach (GameObject spaceStation in spaceStations) { float distance = Vector2.Distance(spaceStation.transform.position, transform.position); if (distance < leastDistance) { leastDistance = distance; nearestSpaceStation = spaceStation; } } //Start moving toward this object. DirectTo(nearestSpaceStation.transform); //Set the appropriate state. state = DirectableState.MovingToSpaceStation; }