提示コード画像ですが右側の武器がプレイヤーの周りを1秒間回ったら瞬間の一回だけtrue
を返したのですがこれをするにはどうゆう実装方法があるのでしょうか? weapon.cs
のようにbool型
変数を使う方法を考えましたが手動でfalse
にするという処理が面倒なのでイベント処理等でスマートに実装したいです。
調べたこと
イベント処理に関して色々調べましたが結局どれを使うのが最適解なのかわかりません。
参考サイト:http://narudesign.com/devlog/unity-eventhandler-return-value/
ソースコードについて
weapon.csが武器についてるソースファイル
weaon_mngが武器の親オブジェクトで武器を管理するクラス
知りたい事
武器がプレイヤーの周りを1秒間回ったら瞬間の一回だけtrue
を返す処理を実装したい。Unity風のやり方が知りたい。
避けたい方法
bool型変数を使ってtrueになった変数をプレイヤーcs側でfalseにする等の複雑な処理
weapon_mng
cs
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class WeaponManager : MonoBehaviour 6{ 7 8 9 GameObject[] weapon; 10 11 // Start is called before the first frame update 12 void Start() 13 { 14 weapon = new GameObject[transform.childCount]; 15 16 for (int i = 0; i < transform.childCount; i++) 17 { 18 weapon[i] = transform.GetChild(i).gameObject; 19 } 20 21 } 22 23 // Update is called once per frame 24 void Update() 25 { 26 27 } 28 29 30 31 /*########################################## 武器 1 ##########################################*/ 32 public GameObject Weapon_1() 33 { 34 return weapon[0].transform.Find("Object").gameObject; 35 } 36 37 /*########################################## 武器 2 ##########################################*/ 38 public GameObject Weapon_2() 39 { 40 return weapon[1].transform.Find("Object").gameObject; 41 } 42 43 /*########################################## 武器 3 ##########################################*/ 44 public GameObject Weapon_3() 45 { 46 return weapon[2].transform.Find("Object").gameObject; 47 } 48 49 /*########################################## 武器 4 ##########################################*/ 50 public GameObject Weapon_4() 51 { 52 return weapon[3].transform.Find("Object").gameObject; 53 } 54 55 /*########################################## 武器 切り替え ##########################################*/ 56 public void ChangeWeapon() 57 { 58 weapon[0].GetComponent<TwinDagger>().Orbit_Animation(); 59 weapon[0].GetComponent<Animation>().Play("orbit_dagger"); 60 61 62 } 63 64} 65
wepaon
cs
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.Events; 5 6public class TwinDagger : MonoBehaviour 7{ 8 public GameObject player; 9 public GameObject camera; 10 [SerializeField] float rotateSpeed = 600; 11 [SerializeField] float orbitSpeed = 2; 12 [SerializeField] float Y_distance = 0.5f; 13 [SerializeField] float distance = 2; 14 15 [SerializeField] Material material_Orbit; // 16 [SerializeField] Material material_Basic; // 17 18 19 UnityEvent event_endOrbit; 20 21 22 public bool isOrbitEnd 23 { 24 get 25 { 26 return isOrbitEnd; 27 } 28 29 set 30 { 31 isOrbitEnd = value; 32 } 33 } 34 35 36 // public GameObject camera; 37 private Animator animator; 38 private float time = 0; 39 // Start is called before the first frame update 40 void Start() 41 { 42 animator = GetComponent<Animator>(); 43 event_endOrbit = new UnityEvent(); 44 event_endOrbit.AddListener(OrbitEnd_Evnet); 45 46 isOrbitEnd = false; 47 48 } 49 50 51 52 53 // Update is called once per frame 54 void Update() 55 { 56 57 Orbit(); //武器軌道 58 59 60 Orbit_Animation(); //武器 切り替え 61 } 62 63 64 65 66 67 68 69 /*########################################## カメラの回転に沿って武器を回転移動 ##########################################*/ 70 private void Orbit() 71 { 72 73 74 Quaternion q = Quaternion.Euler(transform.forward); 75 Vector3 p = Vector3.Normalize(camera.transform.right) * distance; 76 77 Vector3 pDash = q * p; 78 pDash.y = Y_distance; 79 // transform.position = pDash; 80 81 } 82 83 84 85 /*########################################## 武器 切り替え ##########################################*/ 86 public void Orbit_Animation() 87 { 88 transform.Find("Object").GetComponent<Renderer>().material = material_Orbit; 89 90 time += Time.deltaTime; 91 transform.RotateAround(player.transform.position, Vector3.up, orbitSpeed * Time.deltaTime); 92 93 if(time > 1) 94 { 95 transform.Find("Object").GetComponent<Renderer>().material = material_Basic; 96 isOrbitEnd = true; 97 time = 0; 98 } 99 } 100 101 102 /*########################################## 軌道アニメーション終了 ##########################################*/ 103 private void OrbitEnd_Evnet() 104 { 105 106 } 107 108} 109 110
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2022/03/18 10:08
2022/03/18 10:41
退会済みユーザー
2022/03/19 01:55
2022/03/19 02:40