- Home /
How to Make Enemy Follow Local Player only
I have created multiplayer Game. In that game I added Enemy AI. Now enemy follow the player but problem is that Whenever i create or start game and choose LAN host option the code work properly. Enemy follow the player. but when i add another client or another player everything get messed up.camera stop working enemy won't follow the player but as soon as i close all connected client the game again works fine. following is code . please suggest me something.
using UnityEngine; using System.Collections; using UnityEngine.Networking;
public class ArtificialIntelligence : NetworkBehaviour {
public Transform target; // Your Player
public float Maxdistance = 5; // Distance Enemy stops from player
public int MoveSpeed = 5; // How fast The Enemy will move
public int RotationSpeed = 3; // How fast The Enemy will rotate
private Transform mytransform;
void Awake ()
{
mytransform = transform;
}
void Update ()
{
//if(isLocalPlayer)
{
GameObject go = GameObject.FindGameObjectWithTag ("Player");
target = go.transform;
Debug.DrawLine (mytransform.position, target.position, Color.yellow);
if (Vector3.Distance (target.position, mytransform.position) < 20)
{
//Vector3 direction = target.position - mytransform.position;
mytransform.rotation = Quaternion.Slerp (mytransform.rotation, Quaternion.LookRotation (target.position - mytransform.position), RotationSpeed * Time.deltaTime);
Debug.Log ("Hii" + Vector3.Distance (target.position, mytransform.position));
if (Vector3.Distance (target.position, mytransform.position) < Maxdistance)
{
Debug.Log ("Hii" + Vector3.Distance (target.position, mytransform.position));
mytransform.position += mytransform.forward * MoveSpeed * Time.deltaTime;
}
}
}
}
}
thank you
Answer by Varniukas · May 10, 2017 at 09:10 PM
void Update (){ if(isLocalPlayer){ return; } //your code goes here
thank you for providing the solution but the thing is if(isLocalPlayer) work for current object and i want to check "isLocalPlayer" for player.that is i want to check whether player is local or not. how to check that
First of all i started to develop $$anonymous$$ultiplayer game from their only. Now i have completed all those basic things. now i am adding new things on my own and i am stuck here.because there is only one non networked zombi and i want it to follow only local player whoever goes near to him. or should i have to create enemy on client side so that everyone will have their own enemy and that will follow them locali. If it is so then tell me how to spawn enemy at client side. @Varniukas
Answer by adipatil3517 · May 11, 2017 at 01:13 PM
First of all i started to develop Multiplayer game from their only. Now i have completed all those basic things. now i am adding new things on my own and i am stuck here.because there is only one non networked zombi and i want it to follow only local player whoever goes near to him. or should i have to create enemy on client side so that everyone will have their own enemy and that will follow them locali. If it is so then tell me how to spawn enemy at client side.@Varniukas
Your answer
Follow this Question
Related Questions
UnityWebRequest post freezes in WebGL build 1 Answer
Unity Multiplayer game not working 0 Answers
How to check for a host in a certain area in a open world game? 0 Answers
how to sent all players to next level(lan server) 0 Answers
Unet Procedure Calls? 0 Answers