I have a game where a Gunner moves on a pre-set path towards a final destination. You play as a Magic Bullet that auto-fires from the Gunner's current position and can be controlled with WASD. When the bullet collides with a Target, it's destroyed.
My question: How do I change the speed of the Gunner after all of the Targets are destroyed? The idea if for the Gunner to hurry to the end, instead of skilled players waiting around.
Any help would be greatly appreciated. Here's what I have so far:
using UnityEngine;
using System.Collections;
public class MovementPathScript : MonoBehaviour {
public GunnerPathScript PathToFollow;
public int CurrentWayPointID = 0;
private float speedVariable;
public float gunnerSpeed;
public float hurrySpeed;
private float reachDistance = 1.0f;
public float rotatationSpeed = 5.0f;
public string pathName;
public static Quaternion gunnerRotation;
private Vector3 last_position;
private Vector3 current_position;
void Start () {
last_position = transform.position;
}
void Update () {
float distance = Vector3.Distance (PathToFollow.path_objs [CurrentWayPointID].position, transform.position);
GunnerMove ();
PointTowards ();
if (distance <= reachDistance) {
CurrentWayPointID++;
}
if (CurrentWayPointID >= PathToFollow.path_objs.Count) {
CurrentWayPointID = 13;
}
}
void GunnerMove(){
transform.position = Vector3.MoveTowards (transform.position, PathToFollow.path_objs [CurrentWayPointID].position, Time.deltaTime * speedVariable);
speedVariable = gunnerSpeed;
if (GameObject.FindGameObjectsWithTag ("Target") == null) {
speedVariable = hurrySpeed;
//Unity doesn't recognize this, and maintains gunnerSpeed
}
}
void OnTriggerEnter(Collider other){
if (other.gameObject.CompareTag ("Target"))
Destroy (this.gameObject);
}
public void PointTowards(){
var gunnerRotation = Quaternion.LookRotation (PathToFollow.path_objs [CurrentWayPointID].position - transform.position);
transform.rotation = Quaternion.Lerp(transform.rotation, gunnerRotation, Time.deltaTime * rotatationSpeed);
}
}
List
to determine target then at least implement some delegates that will notify this script when target destroys. In that listener (delegate) you can checkif (GameObject.FindGameObjectsWithTag ("Target").Length == 0)
as mentioned in answer below. But please do not useGameObject.FindGameObjectsWithTag
inUpdate
as it finds in whole hierarchy and you are executing it inUpdate
\$\endgroup\$ – Hamza Hasan Apr 7 '16 at 11:51