前提・実現したいこと
targetLockedというboolの変数をfalseにしたい
発生している問題・エラーメッセージ
targetLockedというboolの変数がfalseになってくれない
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Turret : MonoBehaviour 6{ 7 private GameObject target; 8 public bool targetLocked = false; 9 10 public GameObject Bullet; 11 public float fireTimer; 12 public bool shootReady; 13 public GameObject turretTop; 14 public GameObject bulletSpwanPoint; 15 // Start is called before the first frame update 16 void Start() 17 { 18 shootReady = true; 19 } 20 21 // Update is called once per frame 22 void Update() 23 { 24 if (targetLocked) 25 { 26 turretTop.transform.LookAt(target.transform); 27 turretTop.transform.Rotate(0, -0, 0); 28 29 if (shootReady) 30 { 31 Shoot(); 32 } 33 } 34 } 35 36 void Shoot() 37 { 38 Transform _bullet = Instantiate(Bullet.transform, bulletSpwanPoint.transform.position, Quaternion.identity); 39 _bullet.transform.rotation = bulletSpwanPoint.transform.rotation; 40 shootReady = false; 41 StartCoroutine(FireRate()); 42 } 43 44 IEnumerator FireRate() 45 { 46 yield return new WaitForSeconds(fireTimer); 47 shootReady = true; 48 } 49 private void OnTriggerStay(Collider other) 50 { 51 if (other.tag == "Enemy") 52 { 53 if (target) 54 targetLocked = true; 55 else 56 targetLocked = false; 57 58 if (targetLocked == false) 59 { 60 target = other.gameObject; 61 } 62 } 63 } 64 private void OnTriggerExit(Collider other) 65 { 66 targetLocked = false; 67 } 68} 69
試したこと
OntriggerEnterのIF文がうまくいってないのはわかります