Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Newbie question but how do I make an enemy target a singular person?

Discussion in 'Multiplayer' started by jlorenzi, May 31, 2023.

  1. jlorenzi

    jlorenzi

    Joined:
    May 2, 2021
    Posts:
    270
    I'm trying to make an enemy that targets one person until someone else attacks it, the one person would be the person who activated the enemy by getting close to it, how would I do this?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class DefaultEnemy : MonoBehaviourPunCallbacks
    7. {
    8.     public float speed;
    9.     public float activateDistance;
    10.      
    11.     private bool activated;
    12.     private GameObject[] networkHeads;
    13.  
    14.     private void Update()
    15.     {
    16.         networkHeads = GameObject.FindGameObjectsWithTag("Network Head");
    17.        
    18.         foreach ( var head in networkHeads )
    19.         {
    20.             if ( Vector3.Distance(transform.position, head.transform.position) > activateDistance )
    21.             {
    22.                 photonView.RPC("EnableEnemy", RpcTarget.AllBuffered);
    23.             }
    24.         }
    25.  
    26.         if ( activated )
    27.         {
    28.             // do stuff
    29.         }
    30.     }
    31.  
    32.     [PunRPC]
    33.     void EnableEnemy()
    34.     {
    35.         activated = true;
    36.     }
    37. }