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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Making Enemy Chase Closest Player (MultiPlayer)

Discussion in 'Scripting' started by Shears, Aug 23, 2015.

  1. Shears

    Shears

    Joined:
    Jul 7, 2015
    Posts:
    24
    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 UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AI : MonoBehaviour {
    5.  
    6.     public Transform target;
    7.     public int Maxdistance;
    8.     public int MoveSpeed = 5;
    9.     public int RotationSpeed = 3;
    10.    
    11.     private Transform mytransform;
    12.  
    13.     void Awake ()
    14.     {
    15.         mytransform = transform;
    16.     }
    17.    
    18.     void Start()
    19.     {
    20.         GameObject go = GameObject.FindGameObjectWithTag("Player");
    21.         target = go.transform;
    22.         Maxdistance = 2;
    23.     }
    24.    
    25.     // Update is called once per frame
    26.     void Update ()
    27.     {
    28.         Debug.DrawLine(mytransform.position,target.position,Color.yellow);
    29.        
    30.         mytransform.rotation = Quaternion.Slerp(mytransform.rotation,Quaternion.LookRotation(target.position - mytransform.position),RotationSpeed * Time.deltaTime);
    31.        
    32.         if (Vector3.Distance(target.transform.position, mytransform.position) > Maxdistance)
    33.         {
    34.             mytransform.position += mytransform.forward * MoveSpeed * Time.deltaTime;
    35.         }  
    36.     }
    37. }
    38.  
    I will worry about the attacking once I get this part figured out(if your wondering)
     
  2. Shears

    Shears

    Joined:
    Jul 7, 2015
    Posts:
    24
    After going over it I realized That the Start() function needed to be in the Update() function so It will continue to look for a new Transform, rather than only looking for one at the start of the game...

    About to do some tests to see if it will actually switch between players

    So I deleted the Start function and just added the lines to the update like this


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AI : MonoBehaviour {
    5.  
    6.     public Transform target;         // Your Player
    7.     public float Maxdistance = 2;          // Distance Enemy stops from player
    8.     public int MoveSpeed = 5;        // How fast The Enemy will move
    9.     public int RotationSpeed = 3;    // How fast The Enemy will rotate
    10.    
    11.     private Transform mytransform;
    12.  
    13.     void Awake ()
    14.     {
    15.         mytransform = transform;
    16.     }
    17.  
    18.     void Update ()
    19.     {
    20.         GameObject go = GameObject.FindGameObjectWithTag("Player");
    21.         target = go.transform;
    22.  
    23.         Debug.DrawLine(mytransform.position,target.position,Color.yellow);
    24.        
    25.         mytransform.rotation = Quaternion.Slerp(mytransform.rotation,Quaternion.LookRotation(target.position - mytransform.position),RotationSpeed * Time.deltaTime);
    26.        
    27.         if (Vector3.Distance(target.transform.position, mytransform.position) > Maxdistance)
    28.         {
    29.             mytransform.position += mytransform.forward * MoveSpeed * Time.deltaTime;
    30.         }  
    31.     }
    32. }
    33.  
     
  3. Shears

    Shears

    Joined:
    Jul 7, 2015
    Posts:
    24
    OK after Testing, The enemy is in the game when one player spawns, but if a second player spawns the zombie disapeers... im guessing because the zombie has two players to pick from and cannoth pick so it just destroys itself?





    ANY help is appreciated! thanks