#helping MERIDIAN
1 messages · Page 1 of 1 (latest)
ok, post some code
can you post the whole chooserandomplayer class, and how you're trying to update it in your player script
ok
This is ChooseRandomPlayer
public class ChooseRandomPlayer : MonoBehaviour
{
public List<GameObject> listOfPlayers;
// Start is called before the first frame update
void Start()
{
listOfPlayers = new List<GameObject>();
}
void FixedUpdate()
{
Debug.Log(listOfPlayers.Count);
}
}
My CameraFollow script is how I'm going to update it since its the only place where I have a reference to each player that joins into the game.
void FixedUpdate()
{
if (player == null)
{
foreach (PlayerMovement playerMovement in FindObjectsOfType<PlayerMovement>())
{
if (playerMovement.isLocalPlayer)
{
player = playerMovement.gameObject;
offset = transform.position - playerPosition;
}
}
}
if (player != null)
{
chooseRandomPlayer.listOfPlayers.Add(player);
Vector3 playerPosition = player.transform.position;
Vector3 camPosition = playerPosition + offset;
transform.position = Vector3.Lerp(transform.position, camPosition, smoothing * Time.deltaTime);
}
}
and you're getting the error on the line chooseRandomPlayer.listOfPlayers.Add(player);
is chooseRandomPlayer set to somethign that isnt null?
I have it attached to a empty gameobject
yes, but how is it referenced in the camerafollow script
ChooseRandomPlayer chooseRandomPlayer;
ya, thats a null
Oh
well I don't have an error anymore but I have the same issue as before ;/ It keeps adding 1 to the list.
right, because every time you go into fixed update, which is 30 times a second, it does that?
yea.
ok cool lemme see if it works when I have more than 1
um okay it didn't
i think its because since player is not null, it wont add anymore
ok, so now everything is working?
no errors but I need to change the code because it's not adding if more than 1 player is in the game
ok, is that because after your foreach loop player is always the same value?
yea
are u still there
if (player == null)
{
foreach (PlayerMovement playerMovement in FindObjectsOfType<PlayerMovement>())
{
if (playerMovement.isLocalPlayer)
{
if (!chooseRandomPlayer.listOfPlayers.Contains(player))
{
player = playerMovement.gameObject;
chooseRandomPlayer.listOfPlayers.Add(player);
}
offset = transform.position - playerPosition;
}
}
}
if (player != null)
{
Vector3 playerPosition = player.transform.position;
Vector3 camPosition = playerPosition + offset;
transform.position = Vector3.Lerp(transform.position, camPosition, smoothing * Time.deltaTime);
}
``` been trying to figure it out but no luck
oh there's already a thread
//outside of update (probably Start)
players = GameObject.FindGameObjectsWithTag("Player");
random = Random.Range(0, players.Length);
void Update ()
{
nav.SetDestination(players[random].transform.position);
}
@lethal geyser
calling it in Update works but it's not great performance-wise to call FindGameObjectsWithTag every frame
How do I get a random number for each enemy? That's what I'm trying to do.
I'm putting this inside my EnemyMovement script so all of them share it.
if every enemy object has their own instance of the script then the result of Random.Range will be different for each
oh i see
Okay well I think it's working now. gonna need to keep testing.
thank you
I'm having another issue if you can help me with that.
how would i get the enemies to follow another player if the other one dies?
you can have them check if the player is dead, and if it is then do the same findwithtag and random.range to get a new player to follow
the check if dead depends on what system you have currently for a player dying, if it's a bool on a playerscript or destroying the object which would make the reference null
I have a isDead bool on another script for it. Would destroying the player be better than using a bool or how would I use the bool so I dont get the reference?
void Update ()
{
Debug.Log("There are " + players.Length + " active players. " + random + " is the random number.");
if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
{
nav.SetDestination(players[random].transform.position);
if(playerHealth.currentHealth <= 0)
{
random = Random.Range(0, players.Length);
nav.SetDestination(players[random].transform.position);
}
}
}
This is on my EnemyMovement.
don't know what better is for you because i don't know your set-up, but if the bool is on a script then yes you need to get a reference
if(playerHealth.currentHealth <= 0)
{
random = Random.Range(0, players.Length);
nav.SetDestination(players[random].transform.position);
}
this isn't going to run because you have && playerHealth.currentHealth > 0)
it would work outside of the first if statement
void Update ()
{
Debug.Log("Current: There are " + players.Length + " active players. " + random + " is the random number.");
if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
{
nav.SetDestination(players[random].transform.position);
}
if (playerHealth.isDead)
{
random = Random.Range(0, players.Length);
playerHealth = players[random].GetComponent<PlayerHealth>();
nav.SetDestination(players[random].transform.position);
Debug.Log("Update: There are " + players.Length + " active players. " + random + " is the random number.");
}
}```
I tried this and in the Update Log, it is still detecting the dead player
in this case even if the player is dead it will still be in players because that list hasn't been updated
so there's the chance that a dead player is selected
also to minimise issues either use playerHealth.isDead or playerHealth.currentHealth > 0, not both