質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

0回答

861閲覧

[Unity] プレイヤーの周りを周回するオブジェクトでカメラの真ん中に来たらを判定する方法が知りたい。

退会済みユーザー

退会済みユーザー

総合スコア0

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2021/08/06 04:45

提示コードのコメント部内部のコードなのですが提示画像のように武器がプレイヤーの周りを周回する処理で武器がカメラの真ん中に来たら消えるという処理を実装したいのですが"武器がカメラの真ん中に来たら"を判定するにはどうしたらいいのでしょうか?

イメージ説明

cs

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System.Linq; 5 6 7 8 9//攻撃管理 10public class Player_Attack_Control : MonoBehaviour 11{ 12 13 [SerializeField, Range(3, 10)] private float instancePositionDistance; //武器が生成される距離 14 [SerializeField, Range(3, 100)] private int objectPoolInstance; //オブジェクトプールの数 15 [SerializeField, Range(0.0f, 3.0f)] private float intervalTime; //攻撃間隔 16 [SerializeField] private List<GameObject> weaponList; //武器リスト 17 18 19 private List<List<GameObject>> instanceWeapon; //オブジェクト 20 private float time; //時間 21 private int listNumber; //選択中の武器 22 private GameObject weaponInstancePosition; //武器の生成場所 23 private Vector3 forward; //前方向 24 private bool isInstance; //生成するかどうか? 25 private GameObject nowInstance; //現在選択している武器 26 private bool isChangeWeapon; //武器切替 27 private List<GameObject> weapon; //武器切替オブジェクト 28 private Vector3 range; 29 30 31 private List<float> angle; 32 private float rotateSpeed = 50f; 33 private Vector3 rotateDistance; 34 35 36 public void Start() 37 { 38 rotateDistance = new Vector3(3,3,3); 39 time = 0; 40 isInstance = true; 41 weapon = new List<GameObject>(); 42 angle = new List<float>(); 43 44 45 weaponInstancePosition = transform.Find("WeaponInstancePosition").gameObject; 46 instanceWeapon = new List<List<GameObject>>(); 47 48 49 foreach (GameObject g in weaponList) 50 { 51 instanceWeapon.Add(new List<GameObject>()); 52 53 for (int i = 0; i < objectPoolInstance; i++) 54 { 55 instanceWeapon.Last().Add(Instantiate(g, weaponInstancePosition.transform.position, weaponInstancePosition.transform.rotation)); 56 instanceWeapon.Last().Last().SetActive(false); 57 58 } 59 } 60 61 float r = 0; 62 for (int i = 0; i < weaponList.Count(); i++) 63 { 64 weapon.Add(CreateObject(i, weaponInstancePosition.transform.position, weaponInstancePosition.transform.rotation)); 65 angle.Add(r); 66 r += 20; 67 } 68 69 listNumber = 0; 70 } 71 72 73 public void Update() 74 { 75 Attack(); //攻撃 76 Select(); //選択 77 SelectAnimaton(); //アニメーション 78 } 79 80 81 82 //攻撃 83 public void Attack() 84 { 85 Vector3 f = forward; 86 f.y = 0.20f; 87 88 weaponInstancePosition.transform.position = transform.position + f * instancePositionDistance; 89 90 if (nowInstance != null) 91 { 92 nowInstance.transform.position = weaponInstancePosition.transform.position; 93 nowInstance.transform.rotation = Quaternion.LookRotation(f) * Quaternion.Euler(90, 0, 0); 94 } 95 96 if (Input.GetMouseButtonDown(0)) 97 { 98 if ((time >= intervalTime) && (isInstance == false)) 99 { 100 nowInstance.transform.rotation = Quaternion.LookRotation(f) * Quaternion.Euler(90, 0, 0); 101 nowInstance.GetComponent<Bullet>().Active(f); 102 isInstance = true; 103 time = 0; 104 } 105 } 106 107 108 } 109/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 110 //武器選択アニメーション 111 public void SelectAnimaton() 112 { 113 if (isChangeWeapon == true) 114 { 115 116 117 for (int i = 0; i < weaponList.Count(); i++) 118 { 119 weapon[i].gameObject.SetActive(true); 120 weapon[i].gameObject.GetComponent<Bullet>().ChangeViewMaterial(); 121 weapon[i].gameObject.transform.position = transform.position + Quaternion.Euler(0f, angle[i], 0f) * rotateDistance; 122 angle[i] += rotateSpeed * Time.deltaTime; 123 124 if (angle[i] > 360) 125 { 126 angle[i] = 0; 127 } 128 129 130 131 } 132 } 133 } 134/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 135 //武器選択 136 public void Select() 137 { 138 if (Input.GetKeyDown(KeyCode.Q)) 139 { 140 if (listNumber > 0) 141 { 142 isChangeWeapon = true; 143 144 nowInstance.gameObject.SetActive(false); 145 listNumber--; 146 nowInstance = CreateObject(listNumber, weaponInstancePosition.transform.position, weaponInstancePosition.transform.rotation); 147 nowInstance.gameObject.GetComponent<Bullet>().SetActiveInstanceEffect(intervalTime); 148 } 149 } 150 else if (Input.GetKeyDown(KeyCode.E)) 151 { 152 if (listNumber < weaponList.Count - 1) 153 { 154 isChangeWeapon = true; 155 156 nowInstance.gameObject.SetActive(false); 157 listNumber++; 158 nowInstance = CreateObject(listNumber, weaponInstancePosition.transform.position, weaponInstancePosition.transform.rotation); 159 nowInstance.gameObject.GetComponent<Bullet>().SetActiveInstanceEffect(intervalTime); 160 } 161 } 162 163 if (isInstance == true) 164 { 165 nowInstance = CreateObject(listNumber, weaponInstancePosition.transform.position, weaponInstancePosition.transform.rotation); 166 nowInstance.gameObject.GetComponent<Bullet>().SetActiveInstanceEffect(intervalTime); 167 isInstance = false; 168 } 169 } 170 171 public void FixedUpdate() 172 { 173 Vector3 f = Vector3.Scale(Camera.main.transform.forward.normalized, new Vector3(1, 0, 1)).normalized; 174 forward = (f + Camera.main.transform.forward).normalized; 175 176 time += Time.deltaTime; 177 } 178 179 180 //オブジェクトプール オブジェクト生成 181 GameObject CreateObject(int select, Vector3 pos, Quaternion rot) 182 { 183 foreach (GameObject gg in instanceWeapon[select]) 184 { 185 if (gg.activeSelf == false) 186 { 187 gg.SetActive(true); 188 gg.gameObject.GetComponent<Bullet>().Init(); 189 190 191 gg.transform.position = pos; 192 gg.transform.rotation = rot; 193 194 return gg; 195 } 196 } 197 198 instanceWeapon[select].Add(Instantiate(weaponList[select])); 199 instanceWeapon[select].Last().SetActive(true); 200 instanceWeapon[select].Last().transform.position = pos; 201 instanceWeapon[select].Last().transform.rotation = rot; 202 instanceWeapon.Last().Last().gameObject.GetComponent<Bullet>().Init(); 203 204 return instanceWeapon.Last().Last().gameObject; 205 } 206} 207

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問