前提・実現したいこと
2Dのシューティングゲームを作成しています。
右スティックの向きでアニメーションで矢印の向きが変わるようにしました。
矢印の向きに弾を発射させたい。
発生している問題・エラーメッセージ
float型にtransform.rotation.eulerAngles.zを代入してその値の角度に弾を発射させようとし、右スティックを動かしてみると0が代入されています。
該当のソースコード
lookオブジェクトの向きを読み取るスクリプト
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerController : MonoBehaviour { 6 7 public float speed; //移動の速さ 8 9 float horizontal; 10 float vertical; 11 float horizontal2; 12 float vertical2; 13 14 Vector3 velocity; 15 GameObject look; //矢印 16 17 public Shot shotPrefab; // 弾のプレハブ 18 public float shotSpeed; // 弾の移動の速さ 19 public float shotAngleRange; // 複数の弾を発射する時の角度 20 public int shotCount; // 弾の発射数 21 22 float CT1 = 0.5f; // クールタイム 23 bool Attack1; 24 25 public float angles; 26 27 void Start () { 28 29 } 30 31 void Update () { 32 look = GameObject.Find("look"); 33 34 //矢印キーの入力情報を取得する 35 horizontal = Input.GetAxis("Horizontal"); 36 vertical = Input.GetAxis("Vertical"); 37 38 //右スティック 39 horizontal2 = Input.GetAxis("Horizontal2"); 40 vertical2 = Input.GetAxis("Vertical2"); 41 42 //矢印キーが押されている方向にプレイヤーを移動する 43 velocity = new Vector3(horizontal, vertical) * speed; 44 transform.localPosition += velocity; 45 46 //矢印の向きを取得 47 angles = look.transform.rotation.eulerAngles.z; 48 Debug.Log(angles);
lookオブジェクトのアニメーション操作のスクリプト
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class AimController : MonoBehaviour { 6 7 private Animator Aim; 8 private GameObject Player; 9 10 public float h; 11 public float v; 12 13 void Start () { 14 Aim = GetComponent<Animator>(); 15 } 16 17 // Update is called once per frame 18 void Update () { 19 Player = GameObject.Find("Player1"); 20 gameObject.transform.position = Player.transform.position; 21 22 // 右スティックの入力取得 23 h = Input.GetAxis("Horizontal2"); 24 v = Input.GetAxis ("Vertical2"); 25 26 //モーション判定用のパラメータ 27 Aim.SetFloat("Direction_x", h); 28 Aim.SetFloat("Direction_y", v); 29 } 30}
補足情報(FW/ツールのバージョンなど)
unity 5.6.3f1
回答1件
あなたの回答
tips
プレビュー