問題点
以下のスクリプトを記述したとき、最後のInstantiateの部分の処理のみが通っており、Lvに応じたif文が機能していない。
deltaTimeの初期化ができない。
実装したいこと
押している時間に応じて、弾の種類(形状)を変化させるようにしたい。
今回の場合、Lv1 → 押したら
Lv2 → 2~4秒間の間
Lv3 → それ以降
という流れを組もうと思っています。
試したこと、やったこと
時間を計測し、時間に応じた処理をしようとした。
bool型の変数を用意し、それぞれの場合に合わせて変えれるようにしようとした。
これらのことを試しましたが、Lv3まで処理が飛んでしまっているので、どのように分岐すれば段階別にすることができるのか案をご教授していただきたいです。
また、再度計測させるためにそれぞれの処理の最後にdeltaTimeを=0(初期化)するようにした。
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class playerCont : MonoBehaviour { 6 7 public int PlayerHP; 8 public int PlayerBUlletGun;//Playerの撃てる弾数設定用 9 public float Playerspped = 3; 10 public float BulletCount; 11 public float bulletSpeed; 12 public bool Lv1; 13 public bool Lv2; 14 public bool Lv3; 15 16 17 public Bullet BulletPrefab;//弾Prefab格納 18 public Transform target_to_Move; 19 public GameObject tarGet; 20 public AudioClip clip; 21 22 private Vector2 prevPos; 23 24 public Transform my_Trans; 25 private Rigidbody2D rb2d; 26 private float delta; 27 private AudioSource audiosource; 28 29 // Use this for initialization 30 void Start () { 31 this.rb2d = GetComponent<Rigidbody2D>(); 32 // this.anim = GetComponent<Animator>(); 33 this.audiosource = GetComponent<AudioSource>(); 34 35 } 36 37 // Update is called once per frame 38 void Update () { 39 if (Input.GetKey(KeyCode.Joystick1Button1)) 40 { 41 delta+= Time.deltaTime ; 42 Lv1 = true; 43 // 泡弾 Lv.1 44 if (delta > 2 && Lv1) 45 { 46 Instantiate(BulletPrefab, prevPos, my_Trans.rotation); 47 delta = 0; 48 Debug.Log("Lv.1"); 49 Lv1 = false; 50 Lv2 = true; 51 52 //泡弾 Lv.2 53 if (delta > 4 && Lv2) 54 { 55 BulletPrefab.transform.localScale = new Vector3(3, 3, 3); 56 Instantiate(BulletPrefab, prevPos, my_Trans.rotation); 57 delta = 0; 58 Lv2 = false; 59 Lv3 = true; 60 Debug.Log("Lv.2"); 61 62 //泡弾 Lv.3 63 if (delta > 7 && Lv3) 64 65 { 66 BulletPrefab.transform.localScale = new Vector3(6, 6, 6); 67 Instantiate(BulletPrefab, prevPos, my_Trans.rotation); 68 delta = 0; 69 Debug.Log("Lv3"); 70 Lv3 = false; 71 72 } 73 } 74 75 } 76 } 77 //ボタンなしの連射機能 78 //delta += Time.deltaTime; 79 //if (delta > BulletCount) 80 //{ 81 // delta = 0; 82 // Instantiate(BulletPrefab, prevPos, my_Trans.rotation); 83 // audiosource.PlayOneShot(clip); 84 // // Debug.Log("生成"); 85 //} 86 87 // } 88 } 89 void setup() 90 { 91 prevPos = this.transform.position; 92 } 93 private void FixedUpdate() 94 { 95 PlayerMove(); 96 } 97 void PlayerMove() 98 { 99 //移動処理 100 float dy = Input.GetAxis("Vertical") * Playerspped; 101 float dx = Input.GetAxis("Horizontal") * Playerspped; 102 rb2d.Sleep(); 103 104 if ((dx > -0.2f && dx < 0.2f) && (dy < 0.2f && dy > -0.2f)) 105 { 106 107 return; 108 } 109 110 rb2d.velocity = new Vector2(dx, dy); 111 112 //スティック傾き取得 113 float angle = Mathf.Atan2(-dx, dy); 114 float p_angle = (angle) / (Mathf.PI * 2) * 360; 115 this.transform.rotation = Quaternion.Euler(0, 0, p_angle); 116 117 if (dy != 0 || dx != 0) 118 { 119 var diff = (target_to_Move.position - transform.position).normalized; 120 transform.rotation = Quaternion.FromToRotation(Vector3.up, diff); 121 122 } 123 124 125 ////角度計算 126 var nowPos = this.transform.position; 127 128 float x = this.transform.position.x - prevPos.x; 129 float y = this.transform.position.y - prevPos.y; 130 131 Vector2 vec = new Vector2(x, y).normalized; 132 133 134 float rot = Mathf.Atan2(vec.y, vec.x) * 180 / Mathf.PI; 135 if (rot > 180) rot -= 360; 136 if (rot < -180) rot += 360; 137 138 prevPos = nowPos; 139 140 // Debug.Log("Angle = " + rot); 141 prevPos = this.transform.position; 142 143 144 // ShootNWay(,bulletSpeed,BulletCount) 145 146 147 } 148 149 private void OnTriggerEnter2D(Collider2D collision) 150 { 151 //Playerのダメージ計算 152 if(collision.gameObject.name == "Enemy") 153 { 154 PlayerHP = PlayerHP - 1; 155 } 156 } 157} 158 159
回答1件
あなたの回答
tips
プレビュー