Unityでシューティング制作をはじめました。
プレイヤーから弾の発射方向を指定し、その発射方向を回転させていく。という軌道を作っているのですが
弾丸のスプライトが発射方向とがうまく合いません。
弾の発射方向は変数 pbAngで一定量回転しているのですが、弾丸のスプライトはそれとは異なる割合で回転しているようです。
数学的な間違いがある模様ですが、突き止めることができていません。ご指摘やアドバイスがあれば教示お願いします。
スクリプトはプレイヤーの操作と弾丸の移動に分かれています。
こちらはプレイヤーのスクリプトです
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerCtrl : MonoBehaviour { public BulletCtrl pB002; private float shottime = 0; //弾の発射間隔 public float ang001 = Mathf.PI / 2;//弾の発射方向 float pbAng = 0.08f; //発射方向の加算分 float speed001 = 0.005f;//弾の速度 public float deg001 = 0.0f; void Start() { } void Update() { shottime -= 1; Pshot(ang001, speed001, deg001); } void Pshot(float angle, float speed, float deg) { if (shottime < 0) { shottime = 200.0f; ang001 += pbAng; deg001 = ang001 * Mathf.Rad2Deg; BulletCtrl bullet = Instantiate(pB002, transform.position, transform.rotation); bullet.shot(angle, speed, deg); } } }
こちらは弾丸を動かし、スプライトを回転させているスクリプトです。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class BulletCtrl : MonoBehaviour { float dx; float dy; float ang; public void shot(float angle, float speed, float deg) { dx = Mathf.Cos(angle) * speed; dy = Mathf.Sin(angle) * speed; ang = deg; } void Update() { transform.Translate(dx, dy, 0); transform.rotation = Quaternion.Euler(0, 0, ang); //この画像回転が発射方向と合わない if (transform.position.x < -5 || transform.position.x > 5 || transform.position.y < -5 || transform.position.y > 5) { Destroy(gameObject); } } }
どうかよろしくお願いします。
まだ回答がついていません
会員登録して回答してみよう