I have an array that adds enemies.
using UnityEngine;
using System.Collections;
using Pathfinding;
public class LookAt : MonoBehaviour {
//public Transform target;
public GameObject tank;
public GameObject turret;
public float NoDelay = 0;
public float fireDelay = 5.0f;
public float speed = 100;
public float slightDelay = 3f;
public GameObject bullet;
public float radius = 50.0f;
public GameObject bulletSpawn;
public LayerMask mask;
public float fireRate = 0;
//public Transform target;
private GameObject target;
void OnTriggerEnter(){
}
void Update(){
Collider[] enemyArray = Physics.OverlapSphere(transform.position, radius, mask);
if(enemyArray.Length > 0){
target = enemyArray[0].gameObject;
InvokeRepeating("targetingSystem", NoDelay, NoDelay);
InvokeRepeating("Shoot", fireRate, slightDelay);
}
if(enemyArray.Length < 1){
turret.transform.rotation = tank.transform.rotation;
target = null;
Invoke("OutOfRange", NoDelay);
}
}
void targetingSystem(){
turret.transform.LookAt(target.transform.position);
}
void Shoot(){
GameObject instance = Instantiate(bullet, bulletSpawn.transform.position, turret.transform.rotation) as GameObject;
if(instance.rigidbody)
{
instance.rigidbody.AddRelativeForce((Vector3.forward).normalized*speed*Time.deltaTime,ForceMode.VelocityChange);
}
Invoke("OutOfRange", NoDelay);
}
void OutOfRange(){
CancelInvoke("targetingSystem");
CancelInvoke("Shoot");
}
}
Id like to make it so if you right click on an enemy, they become the target. Anyone know how to do this?