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 Tocaro · Jan 28, 2012 at 11:11 PM · destroydistancetags

Destroy when out of range issue

Okay so this should be easy enough right? There's a enemy and if when searching for a gameObject with a player tag we find that it's far enough away that this enemy can despawn, it should destroy itself, so here's my code (The function is called on short intervals):

 function WithinDistance()
 {
     Heroes = GameObject.FindGameObjectsWithTag("Player");
     HeroInRange = false;
     for (var Hero : GameObject in Heroes) 
     {
         if(HeroInRange == false)
         {
             if(Mathf.Abs(Hero.transform.position.x - transform.position.x) < 8 && Mathf.Abs(Hero.transform.position.y - transform.position.y) < 5 && Mathf.Abs(Hero.transform.position.z - transform.position.z) < 5)
             {
                 if(Hero.transform.position.x < transform.position.x && Moving == false)
                 {
                     Reverse();
                 }
                 //networkView.RPC("Unlock",RPCMode.All);
                 HeroInRange = true;
                 //print("Hero in Range is true" + Hero.name);
                 //Moving = true;    
             }
             if(MovementType == "Constant")
             {
                 if(Mathf.Abs(Hero.transform.position.x - transform.position.x) > 8 && Mathf.Abs(Hero.transform.position.y - transform.position.y) > 5 && Mathf.Abs(Hero.transform.position.z - transform.position.z) > 5)
                 {
                     //networkView.RPC("Unlock",RPCMode.All);
                     if(HeroInRange == false)
                     {
                         //print("Inside the death" + Hero.name);    
                         Network.Destroy(gameObject);    
                     }
                 }
             }
             if(MovementType == "Fly")
             {
                 if(Mathf.Abs(Hero.transform.position.x - transform.position.x) > 8 && Mathf.Abs(Hero.transform.position.y - transform.position.y) > 5 && Mathf.Abs(Hero.transform.position.z - transform.position.z) > 5)
                 {
                     //networkView.RPC("Unlock",RPCMode.All);
                     if(HeroInRange == false)
                     {
                         
                         //print(gameObject.name + "Die After a while away");
                         Network.Destroy(gameObject);    
                     }
                 }
             }
         }
         //STILL NOT QUITE WORKING? IN MULTI IT ONLY RECOGNIZES SERVER.
     }
 }

However because this is a multiplayer game there are multiple objects with the player tag, and that's causing some problems. If one player is far away from an enemy but another is right next to it, that enemy will still despawn. I would have thought how I coded it would make it so it would stop looping through once it found a hero close enough, but the enemies are still being destroy, and I have no idea why. And yes, this is the code where the enemies are being destroyed. Also this code is only called by the server, not by the clients (I'm 99% sure).

I would love some help on this, it's driving me crazy.

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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The AI-Players do not see new install (Instantiate) object (the ball) after they set the first goal in gates and old object (the ball) was destroyed. 0 Answers

Destroy multiple game objects when player press a button 2 Answers

How can I make this object destroy on Collison? 1 Answer

move between unique tags 0 Answers

Checking for closest gameobject with tag. 5 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