hi I’ve managed to get a list of all game object players having errors trying to find Random.rang in a list. is it possible to get a random player in a list with random.range? for a turret to lookazt.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AITurret : Photon.MonoBehaviour {
//----------------------------------------------------------------------------------------------------------
public List<Transform> Players = new List<Transform>();
public Transform PlayerSelected;
Quaternion realRotation = Quaternion.identity;
private float distance;
//----------------------------------------------------------------------------------------------------------
void Start ()
{
PlayerSelected = null;
}
//----------------------------------------------------------------------------------------------------------
public void AddplayertoList()
{
GameObject[] ItemsInList = GameObject.FindGameObjectsWithTag("Player");
foreach(GameObject _Player in ItemsInList)
{
AddTarget(_Player.transform);
}
}
public void AddTarget(Transform player)
{
Players.Add(player);
}
//----------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------
public void TargetedPlayer()
{
if(PlayerSelected == null)
{
PlayerSelected = Players[Random.Range(0, Mathf.Round(Players.Count - 1))];
if (distance > 400) {
Debug.Log ("Player Is Out Of Range");
TargetedPlayer (); // if the chosen player is out of range select another player.
}
}
//randomly select a target.
}
//----------------------------------------------------------------------------------------------------------
void LateUpdate ()
{
AddplayertoList (); //constently look at add player
TargetedPlayer();
distance = Vector3.Distance(PlayerSelected.transform.position,transform.position);
//if(dist <150)
//{
Vector3 relativePos = PlayerSelected.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();
}
}
}