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 BHS · May 27, 2012 at 01:08 AM · ontriggerentertag

OnTriggerEnter, if weapon tag collides with player tag then do this

Hi,

I'm writing the fighting system for a game I'm working on. I have one problem though.

How do I use OnTriggerEnter with the tag "enemyweapon" to detect when the player gets hit by the enemy's weapon? I need: if enemyweapon tag collides with player tag then do this. I need to get both collisions but it seems the collisions only detect the game object they're attached to.

How can I go about this? My enemies attack differs so that is why I can't have the fighting systems on different scripts.

Comment
Add comment · Show 2
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 BHS · May 27, 2012 at 01:50 AM 0
Share

I'm trying this, but it's not working, this script is attached to the enemy: (this is only the portion need for the collisions)

  function OnTriggerExit(other: Collider)
  { 

 if (other.CompareTag("PlayerSword")&&!isTriggered) 
 { 
     isTriggered=true;
     SwordSwing();
 }
 
 if (other.CompareTag("EnemyWeapon")&&!isTriggered) 
 { 
 
     if(other.player.tag == "Player")
     {
         isTriggered=true;
            EnemySwordSwing();
        }
    }    
  }
avatar image Berenger · May 27, 2012 at 03:36 AM 0
Share

For general understanding of collision events, I just made that small example, to clear things up.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Common_Horse · May 27, 2012 at 05:01 AM

In Java:

 function OnTriggerEnter( other : Collider) {
         if(other.gameObject.tag == "enemyweapon") {
                 //Do stuff here
         }
 }

In C#:

 void OnTriggerEnter( Collider other) {
         if(other.gameObject.tag == "enemyweapon") {
                 //Do stuff here
         }
 }

If you need an explanation of the code, just ask :)

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 GC1983 · May 27, 2012 at 02:17 AM

Yes the OnTriggerEnter/Exit only works for the collision object its attached to. Now if you want to use the same script for different objects, just separate them through booleans.

 var enemyObject : boolean;

var playerObject : boolean;

OnTriggerEnter(collision : Collider)

{

 if(enemyObject)
 {
     if(collision.gameObject.tag == "Player")
     {
         isTriggered = true;
         EnemySwordSwing();
     }
 }
 
 if(playerObject)
 {
     if(collision.gameObject.tag == "Player")
     {
         isTriggered = true;
         SwordSwing()
     }
 }

}

Just put this same script on both the objects needed and mark the appropriate boolean for each one, so the trigger will know which to use. This should do what you need if its what you are needing. Let me know if you have questions.

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 BHS · May 27, 2012 at 02:45 AM 0
Share

Thanks for this but I'm not sure how it works logically. How can either thing detect the other objects if they're aren't assigned with a var : GameObject? They are just boolean checkboxes. Also I'm using other.Collision not an actual collision.

Can you explain further?

avatar image
0

Answer by GC1983 · May 27, 2012 at 02:50 AM

Theyre assigned through the tag. the collision.gameObject.tag == "" is all you need. Just make sure the objects you want to be affected by this script has the proper tag names. The booleans you will manually check before game start to the assigned object. Check the playerObject bool for the player and the enemyObject for the enemies. This will make the if statement within the bool accessible.

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 BHS · May 27, 2012 at 03:16 AM 0
Share

Well the object that has the enemyweapon tag to it is a separate game object. The whole system is attached to the actual enemy.

Also it says it can't find EnemySwordSwing(); you said to add this to another script. If I can send this through script then I think I can get it to work. I'll just set a OnTriggerEnter on the player and have the EnemySwordSwing(); event happen when he gets hit.

If the above doesn't work then how can I send the enemies attack I've entered to the player after he gets hit? The enemies attack varies because I can set it or I would attach a OnTriggerEnter script to the player. Like if I enter 10 attack for one enemy and 30 to another, how can the script detect this? Can I use a get component?

I'm using an equation to subtract the damage taken to the player.

playersStats.playersHealth = playersStats.playersHealth - enemiesAttack + playersStats.playerDefense * .1;

avatar image GC1983 · May 27, 2012 at 03:44 AM 0
Share

Well, if the EnemySwordSwing(); is being called as you had, the function should be in the same script. Otherwise, you would GetComponent to find it and use it. In the end. I would recommend giving the enemy and the player their own scripts. You attach the OnTriggerEnter() script to the weapon that is making the contact with the object.

avatar image BHS · May 27, 2012 at 05:16 AM 0
Share

Yes, but how can I call the enemy's attack through my player if I set it differently to each enemy?

avatar image GC1983 · May 27, 2012 at 06:29 AM 0
Share

You shouldnt have to. You should have the script attached to them. In the end, you should create a script for the player and one for the enemies. Each should do their thing for response of whatever interaction (damage, chase, attack, whatever...). I dont recommend using one script for both of them. This way you can keep track of what script effects what.

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

7 People are following this question.

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

Related Questions

OnTriggerEnter doesn't read a tag of a moving object 1 Answer

How to work with tags ? 1 Answer

Help With OnTriggerEnter (Collider) 1 Answer

Why isnt OnTriggerEnter Tags working. 1 Answer

AI Attack Not Working! W/Video 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