我的方法OnCollisionEnter有问题,我不知道它为什么不能正常工作。不知道如何在Unity3D中使用OnCollisionEnter
这里是我的代码:
void OnCollisionEnter(Collision col){
if (col.gameObject.name == "Bullet" && !bulletEnter)
{
Debug.Log(enemyLives);
enemyLives = enemyLives - 1;
if (enemyLives == 0)
{
Destroy(gameObject);
//Modificamos el GameManager e incrementamos el dinero que poseemos tras la muerte de Enemy.
GameManager.Manager.currentGold(enemyGold);
}
bulletEnter = true;
}
else if (col.gameObject.name == "OtherBullet" && !bulletEnter) {
enemyLives = enemyLives - 2;
if (enemyLives == 0)
{
Destroy(this.gameObject);
GameManager.Manager.currentGold(enemyGold);
}
bulletEnter = true;
}
}
//Cuando sale la colision lo ponemos a false, esto lo realizamos para que unicamente exista un choque con los coliders.
void OnCollisionExit(Collision col)
{
bulletEnter = false;
}
正如你可以看到有,当子弹到达敌人触发应被激活,但它不工作,我使用“的debug.log”,以检查是否它进入方法内部,但没有。
这里我还添加了我的检查员的照片。
你在你的问题中发布的这个脚本的名字是什么,它附在哪里? – Programmer
它命名为Enemy.cs,并附加到预制件称为“敌人”。 – fiticida
好的。把'Debug.Log(“test”);'if语句之外,但在'OnCollisionEnter'里面,让我知道是否有日志。 – Programmer