- Home /
OnTriggerEnter2D often not called
I have a lot of collisions in my game and most of them worked well at first. But I'm noticing a lot of incidents now, where either the Player, an enemy or a projectile just moves throught an object without interacting with it.
Here's the script of an enemy for example:
using UnityEngine;
using System.Collections;
public class WarriorBScript : MonoBehaviour {
public int strenght;
public float mspeed;
public float direction;
public float dir1;
public float dir2;
public bool legendary = false;
WarriorRScript otherWarrior;
public GameObject corpse;
void Start () {
SetDirection ();
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "RedSoldier")
{
otherWarrior = other.GetComponent<WarriorRScript>();
if(strenght <= otherWarrior.strenght)
{
Instantiate (corpse,transform.position,Quaternion.identity);
Destroy (gameObject);
}
}
if(other.tag == "Attack" || other.tag == "AttackRed")
{
if(!legendary)
{
Instantiate (corpse,transform.position,Quaternion.identity);
Destroy (gameObject);
}
}
if(other.tag == "RedSpell")
{
if(!legendary)
{
MagierRScript.redexp --;
Instantiate (corpse,transform.position,Quaternion.identity);
Destroy (gameObject);
}
}
if(other.tag == "Structure" || other.tag == "BlueSoldier" || other.tag == "BlueMage")
{
dir1 = -dir1;
dir2 = -dir2;
}
}
void SetDirection()
{
direction= Random.Range(0,360) * Mathf.Deg2Rad;
dir1 = Mathf.Sin(direction);
dir2 = -Mathf.Cos(direction);
Invoke ("SetDirection",5);
}
// Update is called once per frame
void FixedUpdate () {
transform.Translate(dir1*mspeed/100,dir2*mspeed/100,0f);
}
}
The GameObject executing the script and the object tagged as Structure both have a rigidbody2d and a collider2d with onTrigger enabled.
I also tried moving the Trigger effect to a script on the structure but about every 3rd collision doesn't get recognised.
using UnityEngine;
using System.Collections;
public class StructureScript : MonoBehaviour {
MagierBScript magierBScript;
MagierRScript magierRScript;
WarriorBScript warriorBScript;
WarriorRScript warriorRScript;
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "BlueMage")
{
magierBScript = other.GetComponent<MagierBScript>();
magierBScript.mspeed = - magierBScript.mspeed;
magierBScript.Invoke("revertMspeed",0.1f);
}
if(other.tag == "RedMage")
{
magierRScript = other.GetComponent<MagierRScript>();
magierRScript.mspeed = - magierRScript.mspeed;
magierRScript.Invoke("revertMspeed",0.1f);
}
if(other.tag == "BlueSoldier")
{
warriorBScript = other.GetComponent<WarriorBScript>();
warriorBScript.dir1 = - warriorBScript.dir1;
warriorBScript.dir2 = - warriorBScript.dir2;
}
if(other.tag == "RedSoldier")
{
warriorRScript = other.GetComponent<WarriorRScript>();
warriorRScript.dir1 = - warriorRScript.dir1;
warriorRScript.dir2 = - warriorRScript.dir2;
}
}
}
I've also read, that the rigidbody of nonmoving object starts sleeping after some time, and tried to prevent them from doing so with the following code, but the problem still occured,
using UnityEngine;
using System.Collections;
public class WakeUp : MonoBehaviour {
Rigidbody2D scriptBody;
void Start() {
scriptBody = gameObject.GetComponent<Rigidbody2D>();
}
void Update(){
if (scriptBody.IsSleeping())
scriptBody.WakeUp();
}
}
I really hope, that someone on here knows how to improve the consistency of the OnTriggerEnter2D or how to handle collisions otherwise.
Thank you for your time and for possible answers.
I tested around some more and found out that the collision weren't registered at points, where one moving object collides with a point, where the colliders of two structures met. It also happens a lot if there is a corner and the object collides with two instances of the same collider at the same time. I guess this could also be a fault of my script, since it simply reverts the speed or direction twice and the object therefore just advances forward.
Answer by Happy-Zomby · Aug 28, 2015 at 06:26 PM
Hi, I encountered this problem with 3d trigger entry and the solution i found was to use ontriggerstay, on triggerexit and a boolean
function OnTriggerStay (other : Collider)
{
if(other.gameObject.tag == "Tree")
{
canGatherWood = true;
}
}
function OnTriggerExit(other : Collider)
{
if(other.gameObject.tag == "Tree")
{
canGatherWood = false;
}
}
then I linked a function to the change of boolean... or you could just check the boolean on the trigger stay and run something
function OnTriggerStay (other : Collider)
{
if(other.gameObject.tag == "Tree" && canGatherWood == false)
{
canGatherWood = true;
DoSomething();
}
}
function OnTriggerExit(other : Collider)
{
if(other.gameObject.tag == "Tree")
{
canGatherWood = false;
}
}
hope that helps somewhat.... make sure to change the boolean to false if you destroy the collider