木に攻撃して3回攻撃したら木が消えるちという処理を作りたいのですが一回攻撃した時点で消えてしまいます。これは何を間違えたのでしょうか?OnTriggerExitなので抜けた時実行されるのでつまり三回抜けたら消えるという処理のはずです。Debug.Log()で表示させています。
つまり攻撃して当たった時の最初の一回だけ実行する方法が知りたいです。
両方にボックスコライダーが入っていて両方ともOnTriggerです。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Tree_Damage : MonoBehaviour { int HP = 3; bool HitDamage = false; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { Debug.Log("ああああ"); if(HitDamage == true) { HP += -1; if(HP == 0) { Destroy(transform.root.game!); } HitDamage = false; } } ///////////////////////////////////////////////////////////// private void OnTriggerExit(Collider other) { if(HitDamage == false){ Debug.Log("木にダメージ!!"); HitDamage = true; } } ///////////////////////////////////////////////////////////// }
using System.Collections; using System.Collections.Generic; using UnityEngine; /*武器の手にアタッチ*/ public class Weapon : MonoBehaviour { // public bool onCollision = false; private GameObject attack_mng; private Attack_Mng am; public int Damage = 1; public bool isAttack = false; // Start is called before the first frame update void Start() { attack_mng = GameObject.Find("Knight"); am = attack_mng.GetComponent<Attack_Mng>(); } // Update is called once per frame void Update() { if(am.OnCollision == true) { GetComponent<BoxCollider>().enabled = true; Damage = 1; isAttack = true; }else{ GetComponent<BoxCollider>().enabled = false; Damage = 0; isAttack = false; } } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。