Need help with a basic follow script bug :)

Okay ive made a basic follow player script in a multiplayer fps for a zombie and the script will follow anything with the tag “Player” so say player1 spawns in all zombies follow player1 but when player2 spawns in all the zombies still follow player1 even if you killed every single zombie in the scene.

Your script probably finds first object with a name/tag player in hierarchy and returns it. You will need to add something that will choose which one to follow from all found objects with a tag. Either randomize it on spawn, or some kind of check which one is closer to zombie or anything else you can think of. This function returns multiple objects with tag FindGameObjectsWithTag .

1 Like

FindGameObjectWithTag - get the first one in the scene
FindGameObject_s_WithTag - get all

(just to highlight the difference :P)

1 Like

Yep, like they said, FindGameObjectsWithTag will return a list. So, in theory you could create a list called players and on your start/awake function have it pull up those tags, and use your list elements/index to selectively choose which player.

1 Like
using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;
using Photon;

public class zombieNetworkMover : Photon.MonoBehaviour {
 
    public delegate void ZombieRespawn(float time);
    public event ZombieRespawn RespawnZombie;
 
 
    Vector3 position;
    Quaternion rotation;
    float smoothing = 10f;
    float zombieHealth = 100f;
    Transform player;                  
    NavMeshAgent nav;            
 

IEnumerator UpdateData()
    {
        while (true) {
            transform.position = Vector3.Lerp (transform.position, position, Time.deltaTime * smoothing);
            transform.rotation = Quaternion.Lerp (transform.rotation, rotation, Time.deltaTime * smoothing);
            yield return null;
        }
    }

    void Awake ()
    {
        player = GameObject.FindGameObjectWithTag ("Player").transform;
        nav = GetComponent <NavMeshAgent> ();
    }

    void Update ()
    {
       
        if (zombieHealth > 0 && photonView.isMine) {
         
            nav.SetDestination (player.position);
        }
       
        else {
           
            nav.enabled = false;
        }
    }
     
     
    void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
            if (stream.isWriting) {
                stream.SendNext (transform.position);
            stream.SendNext (transform.rotation);
            stream.SendNext(zombieHealth);
        } else {
            position = (Vector3)stream.ReceiveNext ();
            rotation = (Quaternion)stream.ReceiveNext ();
            zombieHealth = (float)stream.ReceiveNext();
        }
     
    }
 
    [PunRPC]
    public void ZombieGetShot(float damage)
    {
        zombieHealth -= damage;
        if (zombieHealth <= 0)
        {
         
         
            if(RespawnZombie != null)
            {
                RespawnZombie(2f);
            }
         
            PhotonNetwork.Destroy (gameObject);
        }
    }
}

So here is the example of the code could anyone give me an example of how to fix it with the suggestions above? I’m clueless :slight_smile: