Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 AsAnAsterisk · Jun 20, 2017 at 08:58 PM · collisioncolliderscollision detectiontriggershit detection

How to single out specific colliders from another object

Both the player and enemies have colliders for their bodies and for their attacks. I only want damage to be done when the enemy's attack collider intercepts the players body collider or vice versa. However, I don't know how to phrase the script in a way so that this is doable. Everything I've tried either results in repeated attacks having absolutely no impact on health or the player being injured when the enemy stands next to a couch.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class DetectHit : MonoBehaviour {
 
     public Collider self;
     public Collider attack;
     public Collider target;
     public Slider healthbar;
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject != target) return;
 
         healthbar.value -= 20;
     }
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }
 

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

1 Reply

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

Answer by eskivor · Jun 20, 2017 at 11:53 PM

The method I use to do it is easily is to create separated game objects in the editor for each collider you want to do a specific behaviour.

For example you have a player with a collider on his head, and a collider on his body, and if your enemy touch the head or the body it does two different things.

In my hierarchy window, my player will look like this :

alt text

Player

  • Body (with a collider on it)

  • Head (with a collider on it)


Then you have several possibilities :

either your script with OnTriggerEnter function is on the enemy script, then you can use different tags on the player Body and Head to help you check which collider do you collide

or

your script with OnTriggerEnter function is on the player, and you can make two scripts, one on the player body to say what to execute when he collides the enemy and the other on the player head.


player-body.png (1.1 kB)
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 AsAnAsterisk · Jun 21, 2017 at 12:35 AM 0
Share

I appreciate your help, but my question was more about having the collider only interact with a certain other collider, and not some unrelated one on another mesh, you said something about tags that might be what I'm after, but I'm completely new to those. how would I go about assigning tags to certain objects, is it just that thing at the top of the inspector? what would the scripting be like on that?

avatar image eskivor AsAnAsterisk · Jun 21, 2017 at 01:19 AM 0
Share

You can put only one tag per object, that' why I separate my colliders on different objects.

The tag system is a simple generic system to separate object on different categories (Player, Ennemies, Background, etc.)

To add a tag to an object, select it, and in the inspector, just above the tranform component and under the name of the object, there is a "Tag" category with a dropdown button. On the dropdown, it's displayed "Untagged", the default tag of the object, click on it and select the tag you want. To create your own tag, select the last item "Add Tag" and add a name (then don't forget to assign the new tag on your object).

Then, to use the tag in a script :

 if (gameObject.tag == "theNameOfTheTagYouWantToCheck")
 {
     //Do Something ();
 }
avatar image eskivor AsAnAsterisk · Jun 02, 2021 at 01:39 PM 0
Share

You can also use layers.

You can select the layer of each game object on the inspector (and create new layers on purpose).

Then in the Project Settings (Edit -> Project Settings -> Physics (or Physics 2D)) You can edit the layer collision matrix to select which layers can interact each other.

avatar image AsAnAsterisk · Jun 22, 2017 at 07:36 PM 0
Share

O$$anonymous$$! I finally got it, Unity was really finicky about how it wanted it to work. I had to put the player hitbox (as opposed to the enemy attack hitbox). This is my code in case any one has a similar problem.

  void OnTriggerEnter(Collider other)
     {
      if (gameObject.tag != "EnemyAttack")return; {
     
          healthbar.value -= 20;
         }
     }

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

102 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 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 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 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 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 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

get only one colliding body 3 Answers

Does OnTriggerStay don't detect a collision with a non-trigger collider? 1 Answer

How do I use colliders and/or triggers to end the game? 1 Answer

Trying to make object turn red OnTriggerEnter 0 Answers

BoxCollider get stuck when dragging over the border of two BoxColliders 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