Accessing the different position of a game object with same tags

Hey everyone,

I am working on a multiplayer game which every player controls a vehicle. All the vehicles are copies of the model which has the tag “Player”.

I am doing a AOE effect for a weapon that requires me to go through the list of all the players based on position, eliminating those who are not in range and doing calculations for those who are then send an RPC to the clients to deal damage.

I’m having problem accessing the positions of the players and identifying enemies and myself in game.

The damage calculations all work fine when testing alone however, I don’t know how to run though all the other players and identify them to deal the damage. Like how do I lets say there is 5 players on the map and players number 3 in the list is in range, how do I single him out to ask his side to do damage calculations?

Also I’m using photon cloud and don’t quite understand how or using what method to achieve this effect. I know that I want to

  1. Go through list of all players in map.
  2. Only concentrate on those in range
  3. Send a RPC call to respective players to ask them to calculate damage
  4. Deal damage

void DamageCalculator()
{

//Set maximum damage

      damage = 30;

      
    //Get list of players
      GameObject[] Players;
      
    //run through amount of players and listing them out
      for (int i = 1; i <= GameObject.FindGameObjectsWithTag("Player").Length; i++)
      {
          Players = GameObject.FindGameObjectsWithTag("Player");
          print("Players: " + Players);
      }

      //Get distance between mortar position and player position
      float DistanceDiff = Vector3.Magnitude(transform.position) - Vector3.Magnitude(Players.transform.position); print("Bullet Pos: " + Vector3.Magnitude(transform.position));
          print("Player Pos: " + Vector3.Magnitude(Players.transform.position));
          print("Difference: " + DistanceDiff);

          float DamageMulti = (1.0f - (DistanceDiff / 100));            //Get the damage multiplayer number based on distance

          if (DamageMulti > 1.0 || DamageMulti <= 0.0)                  //If out of range
          {
              damage = 0;
              print("Damage Dealt: Out of Range");

          }

          else if (DamageMulti <= 1.0)                                  //If in range
          {

              float temp = (DamageMulti * damage);

              damage = Mathf.RoundToInt(temp);

              print("Damage Dealt: " + damage);

          }
}

One way to find all the characters in range is to use Physic.OverlapSphere with the players’ layer.

From there, you apparently want the damage to fade with the distance.
The magnitude you are calculating right now is the length of the vector [(0,0,0), transform.position], which isn’t relevant. You the length of the vector from the damage emitter (the current gameObject apparently) to the target.

Loop through all the players found by overlapsphere, calcul the distance and apply the damage. It will looks like that :

private void DamageCalculator()
{
    // First, find all the players near by. 1 << 10 is arbitrary, fill it with your own layer, 20f as well
    Collider[] nearbyPlayers = Physics.OverlapSphere( transform.position, 20f, 1 << 10 );
    // Loop through them
    foreach( Collider C in nearbyPlayer )
    {
        Vector3 dir = C.transform.position - transform.position;
        float dist = dir.magnitude;
        //float dist = dir.sqrMagnitude; // More optimized, because no sqrt
        //float dist = dir.x * dir.x + dir.z * dir.z; // Even more optimized for 2D only, choose the one you prefer

        // Here calculate the damage amount as you want

        // Apply to whatever class the damage is applied to
        C.GetComponent< Whatever >().someVar -= damage;
    }
}