- Home /
Trigger collider triggers even though colliders are not touching
I currently have a trigger collider set up on a weapon, and when it touches an enemy character controller, sends the "DealDamage" method. However, the trigger seems to get activated even when the weapon is nowhere near the enemy. The code I have is below:
var damage : int; private var nextFireTime = 0.0; var reloadTime : float= 0.5; var distanceto : float;
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "Enemy" && PlatformerPlayerAnimation.isAttacking
&& Time.time > nextFireTime)
{
distanceto = Vector3.Distance(gameObject.transform.position, other.gameObject.transform.position);
print(distanceto);
nextFireTime = Time.time + reloadTime;
print("hitboss");
other.gameObject.SendMessage ("DealDamage", damage);
}
When the trigger gets activated, it prints the distance between the two gameobjects so I can make sure that they are definately not touching. I can get it so that "hitboss" gets printed even though the distance is at least 30 (bigger than the colliders themselves).
Any help?
Answer by Tetrad · Jul 10, 2010 at 05:45 PM
I would suggest doing something like this:
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "Enemy" && PlatformerPlayerAnimation.isAttacking && Time.time > nextFireTime)
{
distanceto = Vector3.Distance(gameObject.transform.position, other.gameObject.transform.position);
print(distanceto);
if( distanceto > 30 )
{
Debug.LogError( "Collison occured when too far away?", this );
}
nextFireTime = Time.time + reloadTime;
print("hitboss");
other.gameObject.SendMessage ("DealDamage", damage);
}
}
And then turning on "error pause" in the console. When your situation happens, it'll pause the game on that frame, and you can see exactly where the thing with the collider is in the world (if you double click on the error in the console it'll highlight the game object in the scene.
Also, try some other strategies here: http://answers.unity3d.com/questions/13827/my-script-isnt-working-what-are-some-ways-i-can-start-debugging
Hey thank you Jesse this helped me too, you're a boss man!
Answer by Ashkan_gc · Jul 10, 2010 at 03:59 PM
you might set the tag of the main player to Enemy by accident! check the tag of your main player or just add another condition that checks if the instanceID of the object that we are colliding with is different from the parent of the weapon.
Thanks, but I dont believe that is a case. I have a bit of code that finds all the gameobjects with the tags marked 'Enemy' and prints them to the console, the only thing it returns being the actual enemy. I'll keep messing with it and see if I get an answer.