Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Make an enemy follow a player through network

$
0
0
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.

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>