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