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.

Unity Multiplayer [SOLVED]Enemy Chase Closest Player

Discussion in 'Multiplayer' started by Hun_el, Aug 6, 2018.

  1. Hun_el

    Hun_el

    Joined:
    Aug 28, 2017
    Posts:
    5
    I have a Script to make an enemy chase a player.. it works in single player but now that I added multiplayer I cant get it to work. How can I make the Zombie find the closest player Transform and get him to start chasing?

    This is what I have now..
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. public class FollowEnemy : NetworkBehaviour
    6. {
    7.  
    8.     public Transform tr_Player;
    9.     public Animator anim;
    10.     float f_RotSpeed = 4.0f, f_MoveSpeed = 4.0f;
    11.     public bool Dur = false;
    12.     public bool Bekle = false;
    13.     public bool AttackYap = false;
    14.     public bool Secildi = false;
    15.     public static bool AttackYap2 = false;
    16.     public int a = 0;
    17.    
    18.  
    19.     void Update()
    20.     {
    21.  
    22.         Secildi = true;
    23.             tr_Player = GameObject.FindGameObjectWithTag("Player").transform;
    24.             transform.rotation = Quaternion.Slerp(transform.rotation
    25.                                       , Quaternion.LookRotation(tr_Player.position - transform.position)
    26.                                       , f_RotSpeed * Time.deltaTime);
    27.      
    28.         /* Move at Player*/
    29.  
    30.  
    31.         if (Dur == false && Bekle == false && Attack_Yedi.SaldiriAltinda2 == false) {
    32.             if ((GameObject.FindGameObjectWithTag("Player").transform.position - this.transform.position).sqrMagnitude < 3 * 3 || Secildi == true) {
    33.                 transform.position += transform.forward * f_MoveSpeed * Time.deltaTime;
    34.                 anim.Play("Run");
    35.                 AttackYap = false;
    36.                 AttackYap2 = false;
    37.             }
    38.         }
    39.         if(Dur == true && Player.Saldir1 == false && AttackYap == false)
    40.         {
    41.             anim.Play("Idle");
    42.         }
    43.         if (Dur == true && Bekle == false && Attack_Yedi.bekle == false && Attack_Yedi.bekle2 == false)
    44.         {
    45.             Bekle = true;
    46.             AttackYap = true;
    47.             AttackYap2 = true;
    48.             Invoke("Bekleme", 0.6f);
    49.             anim.Play("Attack1");
    50.         }
    51.     }
    52.     public void OnTriggerStay(Collider other)
    53.     {
    54.         if (other.gameObject.tag == "Player")
    55.         {
    56.             //Invoke("Durr", 0.1f);
    57.             Durr();
    58.         }
    59.     }
    60.     public void OnTriggerExit(Collider other)
    61.     {
    62.         if (other.gameObject.tag == "Player")
    63.         {
    64.             Invoke("Durma", 0.2f);
    65.         }
    66.     }
    67.     public void Durma()
    68.     {
    69.         Dur = false;
    70.     }
    71.     public void Durr()
    72.     {
    73.         Dur = true;
    74.     }
    75.     public void Bekleme()
    76.     {
    77.         Bekle = false;
    78.     }
    79. }
     
  2. Rujash

    Rujash

    Joined:
    Jan 3, 2014
    Posts:
    36
    Is this ran on all clients or just one? It should be run only on server or master client, in theory.
     
    Hun_el likes this.
  3. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Where is the code that is supposed to compare distances? This looks like your old code with only one player, the call to FindGameObjectWithTag will only return one object, which will be whatever the first one in the hierarchy is.
     
    Hun_el likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,824
    1) In Update you need to be checking if this is the server or not. Don't run all this finding player stuff on the clients, only on the server.

    2) Don't run FindGameObjectWithTag on every update. Cache the reference to the player the zombie is chasing, and periodically update the reference. FindGameObjectWithTag is a very expensive call and should not be run by every zombie on every call to Update.

    3) Don't use FindGameObjectWithTag, use FindGameObjectsWithTag and then determine which of the returned objects is the closest. You could try Vector3.Distance, or find a faster method of comparison.

    But again, don't call that every frame anyways. Call it something like once every 4 seconds or so.

    https://docs.unity3d.com/ScriptReference/Vector3.Distance.html
     
    Hun_el likes this.
  5. Hun_el

    Hun_el

    Joined:
    Aug 28, 2017
    Posts:
    5
    Thanks,I solved problem.