- Home /
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 () {
}
}
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 :
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.
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?
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 ();
}
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.
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
Follow this Question
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