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 Anonymoose54 · Aug 01, 2013 at 04:04 PM · javascripttag

How do I single out one object amongst a group of objects with the same tag?

This question continues from my last post. I'm making a game where you're a zombie, and you, along with your zombie follower guy, have to infect every human. So far, I've gotten any zombies that are created to follow me around, and chase any humans that get close to them. This is my Zombie script (attached to my zombie follower):

 var leader : Transform;
 var speed : float = 1;
 var humans : GameObject[];
 var chasing = false;
 var zombieFollower : Transform;
 var picked = false;
 
 function Start () {
 
 }
 
 function Update () {
     humans = GameObject.FindGameObjectsWithTag("human");
     
     for (var human : GameObject in humans){
         var walkTo = Vector3(leader.position.x, 0, leader.position.z + 10);
         var thisZombie : GameObject = this.gameObject;
         var range : float = Vector3.Distance (thisZombie.transform.position, human.transform.position);
         if (range < 10){
             chasing = true;
             var position : Vector3 = Vector3(human.transform.position.x, 0, human.transform.position.z);
             thisZombie.transform.LookAt(position);
             thisZombie.transform.Translate(speed * Vector3.forward * Time.deltaTime);
             if (range < 2){
                 var spawnPosition : Vector3 = Vector3(human.transform.position.x, human.transform.position.y, human.transform.position.z);
                 Destroy(human);
                 var whatToclone = Instantiate(zombieFollower, spawnPosition, Quaternion.identity);
                 chasing = false;
             }
         } else {
             chasing = false;
             thisZombie.transform.LookAt(walkTo);
         }
     }
 }

The zombie gets confused when two humans come within range. How do I single out one human for the zombie to go after?

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 tw1st3d · Aug 01, 2013 at 04:50 PM 0
Share

Create a target variable, create a target array, that is filled with all humans in range. Then, set the target variable to a random object from the target array. There you go, you're set to one human.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Seizure · Aug 01, 2013 at 04:07 PM

Try this:

 var leader : Transform;
 var speed : float = 1;
 var humans : GameObject[];
 var chasing = false;
 var zombieFollower : Transform;
 var picked = false;
  
 function Start () {
  
 }
  
 function Update () {
     humans = GameObject.FindGameObjectsWithTag("human");
  
     for (var human : GameObject in humans){
        var walkTo = Vector3(leader.position.x, 0, leader.position.z + 10);
        var thisZombie : GameObject = this.gameObject;
        var range : float = Vector3.Distance (thisZombie.transform.position, human.transform.position);
        if (range < 10 && !chasing){
          chasing = true;
          var position : Vector3 = Vector3(human.transform.position.x, 0, human.transform.position.z);
          thisZombie.transform.LookAt(position);
          thisZombie.transform.Translate(speed * Vector3.forward * Time.deltaTime);
          if (range < 2){
              var spawnPosition : Vector3 = Vector3(human.transform.position.x, human.transform.position.y, human.transform.position.z);
                Destroy(human);
           var whatToclone = Instantiate(zombieFollower, spawnPosition, Quaternion.identity);
           chasing = false;
          }
         } else {
            chasing = false;
            thisZombie.transform.LookAt(walkTo);
        }
     }
 }
Comment
Add comment · Show 4 · 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 Anonymoose54 · Aug 01, 2013 at 04:09 PM 0
Share

It didn't really change anything...

avatar image Anonymoose54 · Aug 01, 2013 at 04:11 PM 0
Share

I also need the zombie to target a human that is within range. Any ideas?

avatar image Anonymoose54 · Aug 01, 2013 at 04:33 PM 0
Share

Would it involve storing the current human that is being chased in a variable?

avatar image Anonymoose54 · Aug 01, 2013 at 07:00 PM 0
Share

I'll be gone for the next week.

Still answer, please, I'll read them when I get back.

Thx

avatar image
0

Answer by jacobschellenberg · Aug 01, 2013 at 04:07 PM

You could add a property to the Humans called ID or something of the sort that uniquely identifies each Human.

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
avatar image
0

Answer by nixcs2512 · Aug 01, 2013 at 09:25 PM

You can find closest human each frame but actually it won't work in some cases. In example we have 2 human,A and B, human A is closer to the zombie than B, but A is running away and B is coming to the zombie, the strange thing happened : zombie will chase B instead of A. If B runs and A comes, that happened again. And with a little trick, zombie will like chasing no one. So think again about how it would work.I will say that: we have a boolean variable called locktarget, if(!locktarget) zombie will start moving randomly around and searching for human within range,if it can found someone,say the closest target, it will choose that target as currenttarget and keep chasing it, until the distance between them get over range.

 bool locktarget;
 GameObject[] humans;
 GameObject currenttarget;
 float range;// the range to detect human
 void Update()
 {
  humans = GameObject.FindGameObjectsWithTag("human");
  if(!locktarget)
  {
   //Moving randomly or follow the leader
   float closestrange=Mathf.Infinity;
   GameObject closestObject;
   for(GameObject human in humans)
   {
    float distance = Vector3.Distance(transform.position,human.transform.position);
    if(distance<=closestrange)
    {
     closestrange = distance;
     closestObject = human;
    }
   }
   if(closestrange<=range)
   {
    locktarget=true;
    currenttarget = closestObject;
   }
  }
  else //while locking human
  {
   //chase him
   float distance = Vector3.Distance(transform.position,currenttarget.transform.position);
   if(distance>range)
   {
    locktarget = false;
    currenttarget = null;
   }
  }
 }

You need to finish yourself the chasing code and following leader,etc..And by the way sorry for C# code but i only know C#.Actually this code is really easy to understand , at least when you can code above thing. Too confusing with me.

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Calling A Function When A Variable Changes? Help 1 Answer

controlling rigidbody 2d 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 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