Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by djdrool91 · May 08, 2012 at 10:34 AM · positionphotonrpctagsaoe

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);


           }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
     }
 }


Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image djdrool91 · May 09, 2012 at 02:11 AM 0
Share

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

avatar image djdrool91 · May 09, 2012 at 02:38 AM 0
Share

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#.

avatar image djdrool91 · May 09, 2012 at 03:04 AM 0
Share

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?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing scene while joinin room and RPC buffer in Unity Photon Networking 1 Answer

Photon pun 2 how to get the position of a Photon.Realtime.Player 0 Answers

RPC in unity with Photon not working 2 Answers

"PhotonView with ID 1001 has no method "ChatMessage" that takes 2 argument(s): String, Object[]" ... But it has.... 1 Answer

RPC with Random Numbers! 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges