2017-12-11 18 views
0

Unity3D에서 문제가 있습니다. 나는 플레이어와 적 모두에게 동일한 건강 스크립트를 첨부했습니다. 플레이어가 죽었을 때 게임 오버 메시지를 보여주고 싶지만 죽을 때 메시지 오버 게임이 플레이어와 적 모두에게 나타납니다.화합으로 스크립팅하기 3d

내 코드처럼 보이는 :

public class CharacterStats : MonoBehaviour 
{ 
    // Use this for initialization 
    void Start() 
    { 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     health = Mathf.Clamp (health, 0, 100); 
    } 

    public void damage(float damage) 
    { 
     health -= damage; 
     if(health<=0) 
     { 
      Die(); 
      Application.LoadLevel(gameover); 
     } 
    } 

    void Die() 
    { 
     characterController.enabled = false; 
     if (scriptstodisable.Length == 0) 
      return; 
       foreach (MonoBehaviour scripts in scriptstodisable) 

       scripts.enabled = false; 
       if (ragdollmanger != null) 
        ragdollmanger.Raggdoll(); 
    } 
} 
+0

당신은 당신의 캐릭터 (플레이어 또는 적) 및 쇼 '다이'메시지의 유형을 설명 열거를 추가 할 수 있습니다하는 경우에만 열거의 값 == 플레이어까지 .. (Y)의 작업 엄지 손가락 형제 – V319

답변

2

당신이 플레이어와 적 모두 1 스크립트를 사용하는 것처럼. 당신은 모두 다른 클래스를 가지고 인터페이스를 구현하거나 건강을 구현하는 기본 클래스에서 파생한다 :

public class Character : MonoBehaviour 
{ 
    public float Health; 

    public virtual void Damage(float damageValue) 
    { 
     Health -= damageValue; 
    } 

    public virtual void Die() 
    { 

    } 
} 

public Enemy : Character 
{ 
    public override void Die() 
    { 
     // do enemy related stuff 
    } 
} 

public Player : Character 
{ 
    public override void Die() 
    { 
     // do player related stuff. 
     // like game over screen 
    } 
} 

희망이 도움이 :)

+0

고맙습니다 –

1

당신은 CharacterStats인지 여부를 확인하기 위해 부울을 사용할 수 있습니다 예를 들어 ”Player”이라는 태그를 플레이어 게임 개체에 추가하고 gameObject.tag == “Player”인지 확인하거나 동등하게 게임 개체의 이름을 ”Player”으로하고 확인하려면 gameObject.name을 확인할 수 있습니다.

그런 다음 게임 개체가 플레이어 인 경우에만 게임 오버 메시지 기능을 실행할 수 있습니다 (isPlayer이 참).

public class CharacterStats : MonoBehaviour 
{ 
    bool isPlayer = false; 

    // Use this for initialization 
    void Start() 
    { 
     if(gameObject.tag == “Player”) 
     { 
      isPlayer = true; 
     }  
    } 

    // Update is called once per frame 
    void Update() 
    { 
     health = Mathf.Clamp (health, 0, 100); 
    } 

    public void damage(float damage) 
    { 
     health -= damage; 
     if(health<=0) 
     { 
      if(isPlayer) 
      { 
       // Do Player-only stuff 
      } 

      // Do Stuff for both players and enemies 
     } 
    } 
} 
+0

경우 (gameObject.tag == 'Player); 이 오류가 발생합니다 –

+0

태그는 문자열이며 다음과 같이 큰 따옴표로 묶어야합니다 :'if (gameObject.tag == "Player")'. – padonald

+0

감사합니다. @padonald. 어리석은 오자 였어. @yasirkhan, 지금 당신을 위해 작동 했습니까? – Jakey