- Home /
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
Go through list of all players in map.
Only concentrate on those in range
Send a RPC call to respective players to ask them to calculate damage
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);
}
}
Answer by Berenger · May 08, 2012 at 02:34 PM
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;
}
}
Ok I understand what you're trying to say.
I was using the vector of the collided bullet comparing it to the position of the player and using that distance to see if who is in range but I'll try your method and report back! Thanks a million! =D
Sorry, I don't quite understand what the last parameter of Physics.OverlapSphere does. It says "static function OverlapSphere (position : Vector3, radius : float, layer$$anonymous$$ask : int = kAllLayers) : Collider[]" but I don't quite understand what it does. Sorry I'm not very good at this and it's my first time with C#.
All the damage calculations and player detection works flawlessly! =D
Ok I implemented your method and it works brilliantly however I'm having issues with the applying to enemy damage.
How do I specifically target the enemy that is in range and all while knowing who the bullet owner is and also how do I Sync all this to the server. I'm using photon cloud and I would assume I would need to make an RPC call?
I also called this function upon collision but I am still having problems inflicting the damage when it didn't hit the vehicle but on the floor. (The amount of damage changes correctly but it doesn't inflict)
Edit Ok so I now realised that all my previous damages are synced by RPC calls however they are synced bby tags. So if my weapon hit the vehicle, it will know but it will not detect the hit when it hits the floor. Is there a way to have the damaged synced to all clients while the bullet hits the floor but damages an area?