私は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);
}
}
noone392おかげであなたはすでに私もこのフォーラムを使用する方法を知ってはいけない – Mostafiz
を試してみましたが、どのような私たちを見ます。私は悪いです。私は思うコードを追加しました。 – CSharpNoob
[How To Ask?](http://stackoverflow.com/help/how-to-ask)を参照してください。 – dotctor