#helping MERIDIAN

1 messages · Page 1 of 1 (latest)

celest kite
#

ok, post some code

#

can you post the whole chooserandomplayer class, and how you're trying to update it in your player script

lethal geyser
#

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);
        }
    }
celest kite
#

and you're getting the error on the line chooseRandomPlayer.listOfPlayers.Add(player);

lethal geyser
#

yes

#

even though my camera code works

celest kite
#

is chooseRandomPlayer set to somethign that isnt null?

lethal geyser
#

I have it attached to a empty gameobject

celest kite
#

yes, but how is it referenced in the camerafollow script

lethal geyser
#
ChooseRandomPlayer chooseRandomPlayer;
celest kite
#

ya, thats a null

lethal geyser
#

Oh

celest kite
#

make it a public

#

and then you can set it in the editor

lethal geyser
#

well I don't have an error anymore but I have the same issue as before ;/ It keeps adding 1 to the list.

celest kite
#

right, because every time you go into fixed update, which is 30 times a second, it does that?

lethal geyser
#

yea.

celest kite
#

if (!chooseRandomPlayer.listOfPlayers.Contains(player))

#

put that above it

lethal geyser
#

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

celest kite
#

ok, so now everything is working?

lethal geyser
#

no errors but I need to change the code because it's not adding if more than 1 player is in the game

celest kite
#

ok, is that because after your foreach loop player is always the same value?

lethal geyser
#

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
grand niche
#

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

lethal geyser
#

oh ok

#

I can't put players outside of update

grand niche
#

calling it in Update works but it's not great performance-wise to call FindGameObjectsWithTag every frame

lethal geyser
#

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.

grand niche
lethal geyser
#

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?

grand niche
#

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

lethal geyser
#

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.

grand niche
grand niche
lethal geyser
#
 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

grand niche
#

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