unity3d空间射击游戏生活系统碰撞不起作用
问题描述:
由于我是Unity3D的新手,并开始使用Space Shooter教程。现在我无法为飞船创建一个简单的生命系统,也许这是一个愚蠢的错误,但我一直在它已经几个小时,寻找解决方案。unity3d空间射击游戏生活系统碰撞不起作用
的OnTriggerEnter代码:
void OnTriggerEnter (Collider other)
{
if (other.CompareTag ("Boundary") || other.CompareTag ("Enemy"))
{
return;
}
if (explosion != null) //hit object explosion
{
Instantiate (explosion, transform.position, transform.rotation);
}
if (other.tag == "Player" && playerHealth >= 1) {
playerHealth--;
gameController.SubLive (playerHealth);
}
if (other.tag == "Player" && playerHealth <= 0) {
Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
Destroy (other.gameObject);
gameController.GameOver();
}
Destroy (gameObject); //destroy hit object
gameController.AddScore (scoreValue);
/**/
}
我发现,解决办法是递减的球员健康每一次碰撞发生,然而,一旦玩家用船或小行星撞击敌人它不只是工作的第一次船。触发器是玩家船,敌船和螺栓(从船上开枪)。所有的物体都有刚体。你能否建议我做错了什么? 在此先感谢!
你可以找到非编辑的太空射击游戏脚本Here
答
唉!休息一会儿,心情重新回来后,我设法自己解决这个问题! (更容易,我认为,尽管!)
void OnTriggerEnter (Collider other)
{
if (other.CompareTag ("Boundary") || other.CompareTag ("Enemy"))
{
return;
}
if (explosion != null)
{
Instantiate (explosion, transform.position, transform.rotation);
}
if (other.tag == "Bolt")
{
Destroy (other.gameObject);
}
if (other.tag == "Player")
{
gameController.SubLive(); //if player ship collides asteroid or enemy ship reduces 1 health
if (gameController.isDead == true) //explodes ship once playerHealth is 0
{
Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
gameController.GameOver();
Destroy (other.gameObject);
}
}
gameController.AddScore (scoreValue);
Destroy (gameObject);
}
脚本片中gameController:
<...>
private int playerHealth;
public bool isDead;
<...>
void Start()
{
playerHealth = 3;
isDead = false;
}
<...>
public void SubLive()
{
playerHealth--;
UpdateLives();
if (playerHealth <= 0)
{
isDead = true;
}
}
void UpdateLives()
{
livesText.text = "Lives: " + playerHealth;
}
+0
累了的时候不要写代码。 ;) – Draco18s
是什么'gameController.SubLive'做,哪里'playerHealth'从何而来?另外,是否发生了碰撞物体的爆炸,但不是减少健康,还是不会发生? – code11
从gameController scrip调用SubLive函数,该函数用于更新GUI并保持健康状态。我在'void Start()'函数中声明'playerHealth = 3',在脚本类的开始声明'private int playerHealth;'。一旦玩家击中敌船/小行星,它将夺走1次生命,而'GUIText'则显示为2次。不过,后来我可以,因为我想与玩家的船(并摧毁他们)播放器船舶不引起爆炸开始打尽可能多的敌人的对象,无论是生活'GUIText'降至低于2 – himcgyver
但这个脚本本身连接到小行星,右? – code11