I have a game using the Photon Unity Networking plugin and I’m trying to make an enemy locate and chase my players. The problem is they can’t find the player clone. I know how to do it for singleplayer but not for multiplayer. please help
this is the script. C#
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
private Transform myTransform;
void Awake(){
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
}
// Update is called once per frame
void Update () {
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
//Look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
}
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
Now, this will work if there’s only ever one object with the tag ‘player’, but if it’s a multiplayer game you’ll need to be a bit more specific than that!
You’ll need to have some kind of accessible data structure that contains information on all the valid target transforms, so that your enemies can choose between them to pick the most appropriate one to follow. I recommend registering your players with some kind of ‘GameEngine’ object that manages all of that.