2017-04-05 7 views
0

유니티 3D를 처음 접했고 스페이스 슈터 튜토리얼을 시작했습니다. 이제 저는 우주선을위한 단순한 생명 시스템을 만들 수 없습니다. 어쩌면 바보 같은 실수 일 수도 있습니다. 그러나 이미 몇 시간 동안 해결책을 찾고있었습니다.유니티 3d 스페이스 슈터 라이프 시스템 충돌이 작동하지 않습니다.

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

+0

무엇'gameController.SubLive' 할 않고 어디'playerHealth'에서 오는가? 또한 충돌 한 물체의 폭발이 일어나지 만 건강의 뺄셈이 일어나지 않습니까, 아니면 둘 다 발생하지 않습니까? – code11

+0

건강 상태가 남아있는 GUI를 업데이트하는 gameController 스크립트의 SubLive 함수를 호출합니다. 스크립트 클래스의 시작 부분에서'void Start()'함수와'private int playerHealth;'에'playerHealth = 3'을 선언합니다. 플레이어 함선이 적함/소행성에 닿으면 1 라이프가 사라지고 'GUIText' 쇼는 2 점 남았습니다. 그러나 이후에 나는 플레이어 우주선에서 원하는만큼의 적을 공격 할 수 있습니다. (그리고 그들을 파괴합니다.) 플레이어 우주선의 폭발도 시작되지 않으며 삶의 GUIText도 2보다 떨어지지 않습니다. – himcgyver

+0

그러나이 스크립트 자체는 소행성에 부착되어 있습니다. ? – code11

답변

0

우를 찾을 수 있습니다! 약간의 휴식을 취하고 신선한 마음으로 돌아오고 난 후에 나는 그럭저럭 그것을 나 자신으로 해결할 수 있었다! (내가 생각, 생각하는 것이 쉬웠다!) gameController에서

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); 
} 

스크립트 조각 :

<...> 
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