hi guys, I’m having a bit of trouble getting my enemies to target my characters. On a single character it’s fine, but when I switch character mid stage the enemies still focus on where the first character was/is located. Think of it like Mega Man only swapping characters other than weapons.
The script attached is what I have so far and any help is greatly appreciated.
Thank you!
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public GameObject[] targetAll;
public int rotationSpeed;
public int stopDistance;
public int sightDistance;
public int turnDistance;
private float moveSpeed;
public float moveSpeedAttack;
//float souls = PlayerPrefs.GetInt("Souls");
private Transform myTransform;
void Awake(){
myTransform = transform;
targetAll = GameObject.FindGameObjectsWithTag("Player");
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
}
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
targetAll = GameObject.FindGameObjectsWithTag("Player");
float distance = Vector3.Distance(target.transform.position, transform.position);
Debug.DrawLine(targetAll.position, myTransform.position, Color.red);
if(Vector3.Distance(target.position, myTransform.position) < sightDistance)
{
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
moveSpeed = 0;
}
if(Vector3.Distance(target.position, myTransform.position) < stopDistance)
{
moveSpeed = 0;
}
if(Vector3.Distance(target.position, myTransform.position) > stopDistance)
{
moveSpeed = moveSpeedAttack;
}
if(Vector3.Distance(target.position, myTransform.position) < turnDistance){
//Look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
}
}
}
PS: I’m getting this error if it helps-
error CS1061: Type UnityEngine.GameObject[]' does not contain a definition for
position’ and no extension method position' of type
UnityEngine.GameObject’ could be found (are you missing a using directive or an assembly reference?)