私はUnity3Dを初めて使いました。私は実行時に殺したすべての敵を数えようとしており、OnGUI()
に表示しています。私がしたことは、敵が殺されるたびに増分することです:enemyKilled
。しかし、プログラムの中で何度か...ディスプレイが減少してから消えます。Unity3Dで殺された敵を表示してカウントするC#
public class ShootableBox : MonoBehaviour {
Animator anim;
public int currentHealth = 3;
public int enemyKilled; // Count how many enemies you have killed
public float currentHealthLength;
bool isSinking; // To trigger the dead enemy to sink
public float sinkSpeed = 1f; // Sink speed
void Start() {
anim = GetComponent<Animator>();
currentHealthLength = Screen.width/2;
enemyKilled = 0;
}
void Update()
{
// If the enemy should be sinking...
if(isSinking)
{
transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
}
}
public void Damage(int damageAmount)
{
//subtract damage amount when Damage function is called
currentHealth -= damageAmount;
//Check if health has fallen below zero
if (currentHealth <= 0) {
enemyKilled = enemyKilled + 1;
//if health has fallen below zero, deactivate it
anim.SetTrigger ("isDead");
//transform.GetComponent<NavMeshAgent>().Stop();
// Find and disable the Nav Mesh Agent.
GetComponent <NavMeshAgent>().enabled = false;
// Find the rigidbody component and make it kinematic (since we use Translate to sink the enemy).
GetComponent <Rigidbody>().isKinematic = true;
Invoke ("StartSinking", 2.5f);
} else {
anim.SetTrigger("isHit");
anim.SetTrigger("isRun2");
}
}
public void StartSinking() {
// The enemy should now sink.
isSinking = true;
// After 2 seconds destory the enemy.
Destroy (gameObject, 8f);
}
// My problem is somewhere on this part
public void OnGUI() {
GUI.contentColor = Color.yellow;
if (enemyKilled != 0) {
GUI.Box(new Rect(5, 5, currentHealthLength, 20), "Enemies killed: " + enemyKilled);
}
}
}
実行時に既に敵を殺すことができます。私はちょうど正しい数の死んだ敵を表示するようには思えません。このエラーを修正するにはどうすればよいですか?すべてのあなたの助けをありがとうございました。
このスクリプトはあなたの敵に付けられていますか? (またはこの場合は撃ち可能な箱) –
@ Mr.Bigglesworthはい、持っています。 –
あなたはこのスクリプトをプレイヤーまたは敵につないでいますか?これは大きな違いになります。 –