敵やアイテムとの接触判定(2D)

Rigidbody2Dが必要(isKinematicはon)
BoxCollider2D必要(IsTriggerはon)
当たり判定には、互いのColliderとどちらかのRigidbodyが必要
(通常は動くものにRigidbodyを設定する)
物理エンジンは働いてないので、演算は軽く、positionの直接変更も可能

private void OnTriggerEnter2D (Collider2D other)
{
	if(other.tag == "Exit")
	{
		mapGen.isDoor=true;
		if(mapGen.isKey) mapGen.state=0;
	}
	else if(other.tag == "Key" )
	{
		mapGen.isKey=true;
		GameObject.Find("Exit").GetComponent<SpriteRenderer>().sprite=mapGen.door[6];
		Destroy(other.gameObject);
	}
	else if(other.name == "Tresure" )
	{
		mapGen.myItems += mapGen.floorNo;
		GameObject.Find("mapGen").GetComponent<mapGen>().showItems();
		Destroy(other.gameObject);
	}
	else if( other.tag == "Enemy" )
	{
		if(isMove==false)
			other.GetComponent<Slime>().takeDamage(mapGen.atk);
	}
	else if( other.tag == "Fire" )
	{
		takeDamage(10);
	}
}

タグ:

sample
最終更新:2015年06月21日 10:24