enemy is not following

Options

using UnityEngine;

using Photon.Realtime;

using Photon.Pun;


public class enemy : MonoBehaviourPun

{

  public float speed = 5f;

  public float trackingRange = 10f;

  private Photon.Realtime.Player[] players;


  private void Start()

  {

    players = PhotonNetwork.PlayerList;

    if (players.Length == 0)

    {

      Debug.LogError("No players found in the room");

    }

  }


  void Update()

  {

    // Only track the player if they are within the specified range

    foreach (var player in players)

    {

      if (player.TagObject != null && player.TagObject is Transform playerTransform)

      {

        if (Vector3.Distance(transform.position, playerTransform.position) < trackingRange)

        {

          // Move towards the player

          transform.position = Vector3.MoveTowards(transform.position, playerTransform.position, speed * Time.deltaTime);

          // Look at the player

          transform.LookAt(playerTransform);

        }

      }

    }

  }

}