2016-07-24 13 views
1

私はC#とコーディングの一般性が非常に新しいです。Unityでビヘイビアを使用するにはどうすればよいですか?

私が入手したのは、公開されたintが50(ダメージ)の武器スクリプトです。それから私は敵の健康である別のスクリプトを手に入れました。

今私がしたいことは、武器スクリプトの値を敵のヘルススクリプトに適用することです。私はそれを行う方法が分かりません。

私はそのシンプルなことを知っていますが、このことを理解しようと壁に向かって頭を叩いています。

助けてください!

Weapon.cs

using UnityEngine; 
using System.Collections; 

public class Weapon : MonoBehaviour { 

    static Animator anim; 
    public GameObject hitbox; 
    public int damage = 50; 
    private AudioSource MyAudioSource; 
    private AudioClip WeaponSound; 

    void Start() { 
     anim = GetComponentInParent<Animator>(); 
     MyAudioSource = GetComponent<AudioSource>(); 
     GetComponent<EnemyHealth>().TakeDamage(damage);     
    } 

    void Update() { 
     attack(); 
     block(); 
    } 

    public void attack() { 
     if (Input.GetButtonDown("Fire1")) { 
      GetComponent<EnemyHealth>().TakeDamage(damage); 
      anim.SetBool("IsAttacking", true); 
      hitbox.SetActive(true); 
      Debug.Log("hit"); 
      MyAudioSource.PlayOneShot(WeaponSound); 
     } 
     else { 
      anim.SetBool("IsAttacking", false); 
      hitbox.SetActive(false); 
     } 
    } 

    public void block() { 
     if (Input.GetButtonDown("Fire2")) { 
      anim.SetBool("IsBlocking", true); 
     } 
     else { 
      anim.SetBool("IsBlocking", false); 
     } 
    } 
} 

EnemyHealth.cs:これらは両方とも、C#で(他からisntatiateないものを意味する)、別のメインクラスでインスタンス化されていると仮定すると、

using UnityEngine; 
using System.Collections; 

public class EnemyHealth : MonoBehaviour { 

    public int maxHealth = 100; 
    private int currentHealth;   
    private Animator animator; 

    void Start() { 
     currentHealth = maxHealth; 
     animator = GetComponent<Animator>(); 
    } 

    public void OnTriggerEnter(Collider other) { 
     other.GetComponent<Weapon>().attack(); 
    } 

    public void TakeDamage(int _damage) { 
     currentHealth -= _damage; 
     animator.SetTrigger("IsHit"); 
     if(currentHealth <= 0) { 
      Die(); 
     } 
    } 

    void Die() { 
     animator.SetBool("Isdead", true); 
     Destroy(gameObject); 
    } 
} 
+2

noone392おかげであなたはすでに私もこのフォーラムを使用する方法を知ってはいけない – Mostafiz

+0

を試してみましたが、どのような私たちを見ます。私は悪いです。私は思うコードを追加しました。 – CSharpNoob

+1

[How To Ask?](http://stackoverflow.com/help/how-to-ask)を参照してください。 – dotctor

答えて

1

ます単に '。'を使用してください。クラス内のパブリックエレメント、プロパティ、関数にアクセスするための演算子です。

main() 
{ 
    EnemyHealth myehlth = new EnemyHealth(); 
    Weapon myweapn = new Weapon(); 
    myehlth.TakeDamage(myweapn.damage); 
} 

ここでは、 '。あなたの武器クラスの公衆の被害にアクセスしてから、 '。あなたの健康クラスのTakeDamage関数を公開するoeprator

+0

私は非常にコーディングに新しいので、私はあなたが何を意味するのか分かりません。彼らは自分たちのクラス(別のスクリプト)であり、他のクラスはありません。 – CSharpNoob

+0

私は私がスーパーの新人だと言ったのが好きなので、私のやったことはちょうどでした:Weapon myweapon = new Weapon(); TakeDamage(myweapon.damage)を入れます。そしてそれは働いた! – CSharpNoob

+0

コードの3行をコピーして貼り付けることを前提としています。上記のコードを書いたと仮定していました。コードをコピーして貼り付ける方法を理解できない場合は、ここでプロジェクトを設定する方法を示すのが難しいので、単純な "hello world"の例でC#チュートリアルをお勧めします。 – noone392

0

答えは非常に簡単でした!

main() 
 
{ 
 
    
 
    Weapon myweapn = new Weapon(); 
 
    TakeDamage(myweapn.damage); 
 
}

+0

'C#'にはコードスニペットを使わないでください。それらは 'HTML'、' CSS'、 'JavaScipt'のために使われます。 – ventiseis

関連する問題