hi gang trying o create a turret that constantly looks for players
(since players are constantly being destroyed and created in my scene)
find a random player and look at it.
also need a way so it randomly selects a player from the list at random. could i use random.range for this?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AITurret : Photon.MonoBehaviour {
//----------------------------------------------------------------------------------------------------------
List<Players> player = new List<Players>();
public Transform PlayerSelected;
Quaternion realRotation = Quaternion.identity;
//----------------------------------------------------------------------------------------------------------
void Start ()
{
PlayerSelected = null;
}
//----------------------------------------------------------------------------------------------------------
public void AddplayertoList()
{
//makesurethis is done on masterclient
if (PhotonNetwork.isMasterClient) {
foreach(GameObject.FindGameObjectsWithTag("Player") //add a player for each gameobject with tag
Players.Add (player));
}
}
//----------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------
public void TargetedPlayer()
{
if(SelectedTarget == null)
{
SelectedTarget = Players [0];
// need to be slected at random.
}
}
//----------------------------------------------------------------------------------------------------------
void LateUpdate ()
{
AddplayertoList (); //constently look at add player
TargetedPlayer();
float dist = Vector3.Distance(SelectedTarget.transform.position,transform.position);
//if(dist <150)
//{
Vector3 relativePos = SelectedTarget.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
this.transform.rotation = rotation;
//}
if (photonView.isMine) {
} else {
Quaternion realRotation = Quaternion.identity;
}
}
//----------------------------------------------------------------------------------------------------------
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if (PhotonNetwork.isMasterClient) {
stream.SendNext(transform.rotation);
}
else{
this.realRotation = (Quaternion) stream.ReceiveNext();
}
}
}