- Home /
2D Collision detection not working
So I'm trying to make it so when the character punches the robot, the robot loses health. I'm doing that by having an OnTriggerStay function then it checks if the character is punching and if so it sends an applyDamage message to the robot. Both the player and the robot have rigidbodies and box colliders and the player also has an extra box collider that is a trigger which has a larger x scale then the first box collider. Heres my code for the player:
function OnTriggerStay2D(other: Collider2D)
{
if(Punching === true)
{
other.SendMessage("ApplyDamage", TheDamage,SendMessageOptions.DontRequireReceiver);
}
}
Then my code for the robot:
public var health = 100;
public var Test : boolean = false;
function ApplyDamage(TheDamage : int)
{
Test = true;
health -= TheDamage;
}
function Update () {
if(health < 1)
{
this.enabled = false;
}
}
Comment
Check if the box colliders components are "Box Collider 2D".
If by
this.enabled = false;
you want to enable the entire game object, then you should change it to
gameObject.SetActive(false);