- Home /
Why isn't my tag working (on trigger enter)
I need my enemy to perform some specific action only if the player touches it so I have this
var touch=false;
function Start () {
ouch.enabled = false;
}
function OnTriggerEnter(Other : Collider)
{
if (Other.tag == "Jawny")
{
touch = true;
}
}
function Update () {
if(touch == true){
ouch.enabled= true;
}
}
But it doesn't work, however it works if i take out the "if (Other.tag == "Jawny")" but it does with every game object and I want it to be only with the ones with the "jawny" tag
any ideas? Thanks.
I can answer this if I know more information: Is "Jawny" the playerObject or is it an enemy? Is this script for the enemy or for the playerObject?
I use "Collider" in my OnTriggerEnter() functions--the function returns a "Collider" -- here's the reference:
https://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html
Answer by LucasMadword · Mar 17, 2014 at 09:20 PM
Try this:
var touch = false;
function Start () {
touch.enabled = false;
}
function OnTriggerEnter(hit : Collision){
if (hit.tag == "Jawny"){
touch = true;
}
}
function Update () {
if(touch == true){
touch.enabled= true;
}
}
I think it was because you put "Collider" instead of "Collision".
EDIT It should have worked without. Still try my script and also make sure the tag is Jawny and not the Layer.
Answer by robcbryant · Mar 17, 2014 at 09:18 PM
Regardless here are some things to try:
Is the playerObject or enemy have "isTrigger" checked in their collider settings? One of them needs to be a trigger.
The other issue with triggers--one of them needs to be stationary. Two moving triggers don't collide very well if at all.
A more surefire way to control your collision detection is to get the distance from the enemy to the player:
Update(){
//if distance between player and enemy is less than 1 unit then they are touching
if(Vector3.Distance(enemyObject.transform.position, playerObject.transform.position) < 1.0f) touch = true;
}
You can edit the distance parameter according to size of the colliders.
This is obviously a simpler collision detection then edge detection of a box/mesh but it depends on your needs. You could also check the distances from all 8 corners of your box collider if you wanted more accuracy or select specific points to detect distance from--like a hand/head/feet