- Home /
Targeting system HELP please
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?)
Answer by fafase · Oct 30, 2013 at 07:27 AM
I would use a static Transform.
This transform contains the active player you use it just like you are using target now.
You can then remove everything about targetAll.
In your control script, you should a method that swap players. When you swap just make the static var receives the new Transform. That is it.
If you want to go for a more OOP style:
You can have a game manager which has a Transform for activePlayer. This variable is passed to the enemy on creation so in the Start of the enemy script you access the GM and find that variable.
Your control script updates that variable in the GM which indirectly will also update all enemies at once since it is just a reference. And no worries about performance, you are dealing with references so it is light and fast.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120916im_/https://answers.unity.com/themes/thub/images/avi.jpg)