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 Napalm6b · Nov 25, 2013 at 04:55 AM · aiobjectstagscollisiondetection

AI- trouble making NPC attack effect NPCs differently than player

I'm pretty new to Unity, so please bare with me. I have an invulnerable monster that attacks the player and allies. So I use FindGameObjectsWithTag to chase the player group.

like so:

 var localEnemies : GameObject[];
 localEnemies = GameObject.FindGameObjectsWithTag("opponents"); 

I think this is pretty standard and works without problem. Just to test the code if NPCs are hit they are destroyed and if the player is hit a game over scene is loaded. How can I get the player object recognized outside the tag used for tracking position? Here is the code I'm working with. -

 function OnCollisionEnter(collision : Collision)
 {
     var thing : GameObject = collision.gameObject;
     if(thing.tag == "opponents" || thing.tag == "trap")
     {
         if(thing.tag == "opponent" && thing == GameObject.Find("Player"))
         {
             status = false;
             animation.Play("attackMajor");
             Instantiate(small_flame, thing.transform.position, thing.transform.rotation);
             Application.LoadLevel("GameOver");
         }
         else if(thing.tag == "opponents" && thing != player)
         {
             status = false;
             animation.Play("attackMajor");
             Instantiate(small_flame, thing.transform.position, thing.transform.rotation);
             yield WaitForSeconds(3);
             Destroy(thing);
         }
         else if(thing.tag == "trap")
         {
             status = false;
             animation.Play("rage");
             yield WaitForSeconds(3);
             Instantiate(small_flame, thing.transform.position, thing.transform.rotation);
             Destroy(thing);
         }
 
          speed = 1;
          status = true;
     }
 }

Any help would be really appreciated!

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 Napalm6b · Nov 25, 2013 at 11:37 AM 0
Share

oh geez I'm such a dork there is a typo in the tag of the first if statement "opponent" should be "opponents"! AAARRGH :)

1 Reply

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

Answer by fafase · Nov 25, 2013 at 11:33 AM

Use the name of you player. It is probably name "Player".

 if(thing.name == "Player")

Other way, would be to have a reference to your player. Since it is the only one it is not so annoying. Then you compare the references.

 public GameObject player; // Drag player 

then in the method:

 if(thing == player)

This one is actually faster since you compare a 4 bytes address while the other one is a string comparison that needs to go through each char.

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

16 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

Related Questions

Changing Booleans Throught Certain Distance 1 Answer

Objects do different things when clicked on 1 Answer

Ontrigger not working with npc 0 Answers

Same objects doing the same thing 0 Answers

Changing tags of an Object and its Child 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