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 Infinity · May 07, 2011 at 02:43 PM · targetmonsterchoose

My monster only choose one Target

Hi, I have been using as base, script monsters made by Unity, but now I have a problem, I have a couple of characters on the Game, so it have became a problem for my monsters, they don't know how to choose the Target properly, here you have the part of the code that chooses a player (It always choose the second). What I can do to make my monster consider both players?

if (!target)
    target = GameObject.FindWithTag("Player").transform;

Comment
Add comment · Show 1
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 equalsequals · May 07, 2011 at 04:54 PM 0
Share

If you have every player Game Object in your scene tagged as "Player", FindWithTag() will always return the first Game Object with that tag that it finds.

2 Replies

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

Answer by Ejlersen · May 07, 2011 at 05:48 PM

As equalsequals says, that method will return the first occurence of a game object with the tag "Player".

Besides, you might want to keep an array with all your game objects with the tag "Player", because it can become quite expensive to lookup game objects every time your monsters don't have a target.

Comment
Add comment · Show 1 · 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 Infinity · May 08, 2011 at 11:32 AM 0
Share

Thanks, how can I do it?

avatar image
0

Answer by Ejlersen · May 10, 2011 at 06:23 PM

I'm just going to create a new answer for the comment question above :-)

You could create a manager script to keep track of all players. E.g.:

public class Manager { public List<GameObject> players = new List<GameObject>();

 public GameObject GetClosestPlayer(GameObject monster)
 {
     Vector3 monsterPosition = monster.transform.position;
     float closestDistance = Mathf.Infinity;
     GameObject closestPlayer = null;

     for (int i = 0; i &lt; players.Count; i++)
     {
         GameObject otherPlayer = players[i];
         float tempDistance = (otherPlayer.transform.position - monsterPosition).sqrMagnitude;

         if (closestDistance &lt; tempDistance)
             continue;

         closestPlayer = otherPlayer;
         closestDistance = tempDistance;
     }

     return closestPlayer;
 }

 // Singleton
 private static Manager instance;

 public static Manager Instance
 {
     get
     {
         if (instance == null)
             instance = new Manager();
         return instance;
     }
 }

}

There are several ways of doing it. This one is done by using a Singleton. It can be accessed by all scripts by saying Manager.Instance.GetClosestPlayer(obj);

You do need to add the players to the list as they spawn, and ofcourse remove them if they are removed.

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

how to make a shooting target ? 2 Answers

How do I get an NPC to stop Following a player based on distance? 1 Answer

Camera focusing on an object specified in a script 1 Answer

'Mission Complete/Game Over' screen once objective is complete. 1 Answer

Activating the camera with a toggle button 1 Answer


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