Q&A
前提
Unityで3Dシューティングゲームを制作しております。
昨日に一通りプレイできる所まで到達し正常に動かすことはできたのですが、今日実行したところプレイヤーが自分の撃った弾でダメージを受けるようになってしまいました。
想定している仕組みとしては、プレイヤーと敵のいずれかのチームを設定して自分と違うチームの弾に当たった時にダメージを与えるというものとなっております。
実現したいこと
- ▲▲自分の撃った弾でダメージを受けないようにする
該当のソースコード
操作キャラクターの動作に関するスクリプト
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5using UnityEngine.SceneManagement; 6 7public class ShootingPlayer : BaseCharacter 8{ 9 public Slider HPslider;//上から順にHPバーとつなげる・最大HP・現在のHP 10 float maxhp = 200f; 11 float hp = 200f; 12 13 void Start() 14 { 15 team = Team.PLAYER;//チームをプレイヤーに設定する 16 17 HPslider.maxValue = maxhp;//上記の引数を入れる 18 HPslider.value = hp; 19 } 20 21 void Update()//プレイヤーの動きを設定する 22 { 23 Vector3 movedirection = new Vector3(0, 0, 0); 24 if (Input.GetKey(KeyCode.UpArrow)) 25 { 26 movedirection.y = 1; 27 } 28 if (Input.GetKey(KeyCode.DownArrow)) 29 { 30 movedirection.y = -1; 31 } 32 if (Input.GetKey(KeyCode.RightArrow)) 33 { 34 movedirection.x = 1; 35 } 36 if (Input.GetKey(KeyCode.LeftArrow)) 37 { 38 movedirection.x = -1; 39 } 40 movedirection.Normalize(); 41 movedirection *= speed * Time.deltaTime; 42 transform.position += movedirection; 43 44 if (Input.GetKeyDown(KeyCode.Space))//弾を撃つ 45 { 46 shot(Direction.FRONT); 47 } 48 49 if (life <= 0)//体力が0になった時の処理 50 { 51 Destroy(this.gameObject); 52 Debug.Log("がめおべら"); 53 SceneManager.LoadScene("GameOver"); 54 } 55 } 56 void OnTriggerEnter(Collider other)//ダメージを受けた時の処理 57 { 58 life--;//体力を減らす 59 Debug.Log(life); 60 61 hp = hp - 20;//HPバーを減らす 62 HPslider.value -= 20f; 63 } 64} 65
C#
1public GameObject PlayerObject; 2 protected float power = 1000f; 3 public GameObject bullet; 4 protected Team team; 5 public int life = 10; 6 public float lifeTime = 64.0f; 7 8 [SerializeField]//publicでなくてもinspecterに表示できる 9 protected int attack = 1; 10 11 public enum Direction//弾の発射方向の種類 12 { 13 FRONT, 14 BACK, 15 RIGHT, 16 LEFT 17 } 18 public enum Team//チームの種類 19 { 20 PLAYER, 21 ENEMY 22 } 23 24 protected void shot(Direction direction) 25 { 26 GameObject newbullet = Instantiate(bullet, transform.position, Quaternion.identity) as GameObject;//メモリの場所の確保(弾、発生場所、姿勢制御)型を揃える 27 Shell shell = newbullet.GetComponent<Shell>(); 28 shell.setTeam(GetTeam()); 29 shell.setAttack(attack); 30 31 switch (direction){//弾の発射方向の指定 32 case Direction.FRONT: 33 shell.setDirection(Vector3.forward); 34 break; 35 case Direction.BACK: 36 shell.setDirection(Vector3.back); 37 break; 38 case Direction.RIGHT: 39 shell.setDirection(Vector3.right); 40 break; 41 case Direction.LEFT: 42 shell.setDirection(Vector3.left); 43 break; 44 } 45 } 46 47 public Team GetTeam() 48 { 49 return team; 50 } 51 52 public void OnTriggerEnter(Collider other)//当たった瞬間の処理 53 { 54 Shell shell = other.GetComponent<Shell>(); 55 if (shell == null) 56 { 57 return; 58 } 59 if (shell.getTeam() != GetTeam())//自分のTeamではなかった場合 60 { 61 life -= shell.getAttack(); 62 } 63 } 64}
試したこと
補足情報(FW/ツールのバージョンなど)
・使用したツールのバージョン
Unity:2020.3.4.f1
VisualStudio:Visual Studio 2022
弾以外のオブジェクトはisTriggerを切っております
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。