SS1
1
Hi I’m making a networking game, I can’t get my enemy’s to follow other player than the first player that is spawn in when the server starts. Here is my code :
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class MoveToPlayer : NetworkBehaviour
{
public Transform target; //the enemy's target
public GameObject[] go;
public float moveSpeed = 3f; //move speed
public float rotationSpeed = 3f; //speed of turning
public float range = 20f;
public float range2 = 20f;
private float stop = 5;
public Transform myTransform; //current transform data of this enemy
void Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
void Start()
{
}
void Update ()
{
if (!isLocalPlayer)
{
CmdMoving ();
}
else if (isLocalPlayer)
{
CmdMoving ();
}
else
{
CmdMoving ();
}
}
void CmdMoving ()
{
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
//rotate to look at the player
var distance = Vector3.Distance (myTransform.position, target.position);
if (distance <= range2 && distance >= range)
{
myTransform.rotation = Quaternion.Slerp (myTransform.rotation,
Quaternion.LookRotation (target.position - myTransform.position), rotationSpeed * Time.deltaTime);
}
else if (distance <= range && distance > stop)
{
//move towards the player
myTransform.rotation = Quaternion.Slerp (myTransform.rotation,
Quaternion.LookRotation (target.position - myTransform.position), rotationSpeed * Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance <= stop)
{
myTransform.rotation = Quaternion.Slerp (myTransform.rotation,
Quaternion.LookRotation (target.position - myTransform.position), rotationSpeed * Time.deltaTime);
}
}
}
Thanks in advance.
Hi, this video might give you some ideas: UNET Part 12 - Zombies! - YouTube, there is short tutorial how to make enemy follow an target.
SS1
3
Ok this was my solution for those may need it, it also can shot at a target
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class MoveToPlayer : NetworkBehaviour
{
public Transform target; //the enemy’s target
public float moveSpeed = 3f; //move speed
public float rotationSpeed = 3f; //speed of turning
public float range = 20f;
public float range2 = 20f;
private float stop = 10;
public Transform myTransform; //current transform data of this enemy
private LayerMask raycastLayer;
//Shooting
public GameObject shot;
public Transform shotSpawn;
private int randomCount;
void Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
void Start()
{
raycastLayer = 1 << LayerMask.NameToLayer ("Player"); //target the player
}
void FixedUpdate ()
{
moveToTarget ();
}
void Update ()
{
randomCount = Random.Range (1, 100);
if (!isServer)
return;
var distance2 = Vector3.Distance(myTransform.position, target.position);
if (distance2 <= range)
{
CmdDoFire ();
}
}
void moveToTarget ()
{
if (!isServer)
return;
if (target != null && isServer)
{
Collider[] distance = Physics.OverlapSphere (myTransform.position, range, raycastLayer);
if (distance.Length > 0)
{
int randomint = Random.Range (0, distance.Length);
target = distance[randomint].transform;
}
var distance1 = Vector3.Distance(myTransform.position, target.position);
if (distance1 <= range2 && distance1 >= range)
{
myTransform.rotation = Quaternion.Slerp (myTransform.rotation,
Quaternion.LookRotation (target.position - myTransform.position), rotationSpeed * Time.deltaTime);
}
else if (distance1 <= range && distance1 > stop)
{
//move towards the player
myTransform.rotation = Quaternion.Slerp (myTransform.rotation,
Quaternion.LookRotation (target.position - myTransform.position), rotationSpeed * Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance1 <= stop)
{
myTransform.rotation = Quaternion.Slerp (myTransform.rotation,
Quaternion.LookRotation (target.position - myTransform.position), rotationSpeed * Time.deltaTime);
}
}
}
[Command]
void CmdDoFire ()
{
if (randomCount < 2)
{
GameObject instance = (GameObject)Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
GetComponent<AudioSource> ().Play ();
NetworkServer.Spawn (instance);
}
}
}enter code here
The first thought that comes to mind is in regard to your usage of “FindGameObjectWithTag()”
That function limits you to a single result, which will most likely be the first player spawned with the lowest ObjectID associated with it.
What you should consider is using “FindGameObjectsWithTag()” (note that it’s plural) to get an array of GameObjects instead. Then, you can iterate through them and select a random target, the nearest one, the nearest the enemy has line of sight to, etc.
That said, FindGameObject(s)WithTag() (either singular or plural) is a very expensive operation. I would suggest storing the list of possible player targets whenever a player joins or leaves the game (or dies and respawns, or whatever) and update it only when necessary.
SS1
5
This is my solution was this, I also made it shot
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class MoveToPlayer : NetworkBehaviour
{
public Transform target; //the enemy's target
public float moveSpeed = 3f; //move speed
public float rotationSpeed = 3f; //speed of turning
public float range = 20f;
public float range2 = 20f;
private float stop = 10;
public Transform myTransform; //current transform data of this enemy
private LayerMask raycastLayer;
//Shooting
public GameObject shot;
public Transform shotSpawn;
private int randomCount;
void Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
void Start()
{
raycastLayer = 1 << LayerMask.NameToLayer ("Player"); //target the player
}
void FixedUpdate ()
{
moveToTarget ();
}
void Update ()
{
randomCount = Random.Range (1, 100);
if (!isServer)
return;
var distance2 = Vector3.Distance(myTransform.position, target.position);
if (distance2 <= range)
{
CmdDoFire ();
}
}
void moveToTarget ()
{
if (!isServer)
return;
if (target != null && isServer)
{
Collider[] distance = Physics.OverlapSphere (myTransform.position, range, raycastLayer);
if (distance.Length > 0)
{
int randomint = Random.Range (0, distance.Length);
target = distance[randomint].transform;
}
var distance1 = Vector3.Distance(myTransform.position, target.position);
if (distance1 <= range2 && distance1 >= range)
{
myTransform.rotation = Quaternion.Slerp (myTransform.rotation,
Quaternion.LookRotation (target.position - myTransform.position), rotationSpeed * Time.deltaTime);
}
else if (distance1 <= range && distance1 > stop)
{
//move towards the player
myTransform.rotation = Quaternion.Slerp (myTransform.rotation,
Quaternion.LookRotation (target.position - myTransform.position), rotationSpeed * Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance1 <= stop)
{
myTransform.rotation = Quaternion.Slerp (myTransform.rotation,
Quaternion.LookRotation (target.position - myTransform.position), rotationSpeed * Time.deltaTime);
}
}
}
[Command]
void CmdDoFire ()
{
if (randomCount < 2)
{
GameObject instance = (GameObject)Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
GetComponent<AudioSource> ().Play ();
NetworkServer.Spawn (instance);
}
}
}