問題点
実行した際に、対象オブジェクトに対して自身のオブジェクトが約45°ずれている。
やりたいこと、知りたいこと
生成位置の調整。ずれの修正を行いたい
やってみたこと
Quaternion と Quaternion型 の違いがわからない。
Quartanion型同士での計算でやれば直るのではないか。と思い、以下のコードを考えてみた
c#
1 Quaternion rotetion = new Quaternion(0, 0, 45, 1); 2 Instantiate(Bullet, transform.position, transform.localRotation + rotetion); 3
が、エラーを吐かれ実装できなかった。
ソースコード
c#
1//拡張メソッド 2 public static void LookAt2D(this Transform transform, Vector2 target) 3 { 4 Vector2 current = transform.position; 5 var direction = target - current; 6 var angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; 7 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); 8 } 9
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class BOSS : MonoBehaviour { 6 7 public float speed; 8 9 public Transform Player; 10 11 //弾系統管理変数 12 public GameObject Bullet; 13 [SerializeField] 14 private float timer;//発射時間計測用変数 15 16 //コンポーネント取得 17 18 // Use this for initialization 19 void Start () { 20 } 21 22 // Update is called once per frame 23 void Update () { 24 25 this.transform.LookAt2D(Player.transform.position); 26 27 timer += Time.deltaTime; 28 //一定時間後に発射 29 if (timer > 1f) 30 { 31 Quaternion rotetion = new Quaternion(0, 0, 45, 1); 32 Instantiate(Bullet, transform.position, transform.localRotation); 33 timer = 0; 34 } 35 36 } 37