質問編集履歴
2
誤字の修正
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -160,9 +160,9 @@ | |
| 160 160 | 
             
            		while (true) {
         | 
| 161 161 | 
             
            			//Debug.Log (GetBulletSpeed());
         | 
| 162 162 |  | 
| 163 | 
            -
            			// 弾を | 
| 163 | 
            +
            			// 弾を敵オブジェクトと同じ位置/角度で作成
         | 
| 164 164 | 
             
            			Instantiate (muzzle, transform.position, transform.rotation);
         | 
| 165 | 
            -
            			//  | 
| 165 | 
            +
            			// shotDelay秒待つ
         | 
| 166 166 | 
             
            			yield return new WaitForSeconds (shotDelay);
         | 
| 167 167 | 
             
            		}
         | 
| 168 168 | 
             
            	}
         | 
1
ソースコードの追加
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -12,4 +12,210 @@ | |
| 12 12 | 
             
            弾がちょうどプレイヤーに衝突するよう、弾の上昇値を変えなければならないとは思っているのですが、具体的にどの要素が必要なのかが分かっていない状況です。
         | 
| 13 13 |  | 
| 14 14 | 
             
            ヒントやアドバイス、解決法の提案等、何でも構いませんので、お答え頂きたく思います。
         | 
| 15 | 
            -
            よろしくお願い致します。
         | 
| 15 | 
            +
            よろしくお願い致します。
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            ###追記
         | 
| 18 | 
            +
            文章のみの質問で申し訳ありませんでした。
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            以下に当問題に関係があると考えているスクリプトを添付させて頂きます。
         | 
| 21 | 
            +
            読みにくいコードかとは思いますが、よろしくお願い致します。
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            ```c#
         | 
| 24 | 
            +
            using System.Collections;
         | 
| 25 | 
            +
            using System.Collections.Generic;
         | 
| 26 | 
            +
            using UnityEngine;
         | 
| 27 | 
            +
             | 
| 28 | 
            +
            public class PlayerParameter : MonoBehaviour {
         | 
| 29 | 
            +
             | 
| 30 | 
            +
            	//PlayerのRigidbody2D
         | 
| 31 | 
            +
            	private Rigidbody2D rb2d;
         | 
| 32 | 
            +
            	//Playerの速度
         | 
| 33 | 
            +
            	private float v = 0.0f;
         | 
| 34 | 
            +
            	//Playerの速度ベクトル
         | 
| 35 | 
            +
            	private Vector3 vVector;
         | 
| 36 | 
            +
            	//Playerの現在位置
         | 
| 37 | 
            +
            	private Vector3 nowPos;
         | 
| 38 | 
            +
            	//Playerの前フレームの位置
         | 
| 39 | 
            +
            	private Vector3 prevPos = Vector3.zero;
         | 
| 40 | 
            +
            	//Playerの移動ベクトル
         | 
| 41 | 
            +
            	private Vector3 moveVec = Vector3.zero;
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            	// Use this for initialization
         | 
| 44 | 
            +
            	void Start () {
         | 
| 45 | 
            +
            		//Rigidbody2Dコンポーネントを取得
         | 
| 46 | 
            +
            		rb2d = this.GetComponent<Rigidbody2D> ();
         | 
| 47 | 
            +
            	}
         | 
| 48 | 
            +
            	
         | 
| 49 | 
            +
            	// Update is called once per frame
         | 
| 50 | 
            +
            	void Update () {
         | 
| 51 | 
            +
            		//Playerの現在位置を格納
         | 
| 52 | 
            +
            		nowPos = this.transform.position;
         | 
| 53 | 
            +
            		//Playerの速度値を格納
         | 
| 54 | 
            +
            		v = rb2d.velocity.magnitude;
         | 
| 55 | 
            +
            		//Playerの速度ベクトルを格納
         | 
| 56 | 
            +
            		vVector = rb2d.velocity;
         | 
| 57 | 
            +
            		//Playerの移動ベクトルを格納
         | 
| 58 | 
            +
            		moveVec = nowPos - prevPos;
         | 
| 59 | 
            +
            		//Playerの現在位置を前フレームの位置にする
         | 
| 60 | 
            +
            		prevPos = nowPos;
         | 
| 61 | 
            +
            	}
         | 
| 62 | 
            +
             | 
| 63 | 
            +
            	//Playerの速度を返す関数
         | 
| 64 | 
            +
            	public float GetVelocity(){
         | 
| 65 | 
            +
            		return v;
         | 
| 66 | 
            +
            	}
         | 
| 67 | 
            +
             | 
| 68 | 
            +
            	//Playerの速度ベクトルを返す関数
         | 
| 69 | 
            +
            	public Vector3 GetVelocityVector(){
         | 
| 70 | 
            +
            		return vVector;
         | 
| 71 | 
            +
            	}
         | 
| 72 | 
            +
             | 
| 73 | 
            +
            	//Playerの移動ベクトルを返す関数
         | 
| 74 | 
            +
            	public Vector3 GetMoveVector(){
         | 
| 75 | 
            +
            		return moveVec;
         | 
| 76 | 
            +
            	}
         | 
| 77 | 
            +
            }
         | 
| 78 | 
            +
            ```
         | 
| 79 | 
            +
            ```c#
         | 
| 80 | 
            +
            using System.Collections;
         | 
| 81 | 
            +
            using System.Collections.Generic;
         | 
| 82 | 
            +
            using UnityEngine;
         | 
| 83 | 
            +
             | 
| 84 | 
            +
            public class Enemy : MonoBehaviour {
         | 
| 85 | 
            +
             | 
| 86 | 
            +
            	//標的(Player)
         | 
| 87 | 
            +
            	private GameObject playerObj;
         | 
| 88 | 
            +
            	//自分(Enemy)の座標
         | 
| 89 | 
            +
            	private Vector3 mPos;
         | 
| 90 | 
            +
            	//Playerの座標
         | 
| 91 | 
            +
            	private Vector3 pPos;
         | 
| 92 | 
            +
            	//Playerの移動予測座標(偏差射撃のため)
         | 
| 93 | 
            +
            	private Vector3 forecastPos;
         | 
| 94 | 
            +
            	//Playerの速度
         | 
| 95 | 
            +
            	private float p_velocity;
         | 
| 96 | 
            +
            	//PlayerとEnemy間の距離
         | 
| 97 | 
            +
            	private float p_distance;
         | 
| 98 | 
            +
             | 
| 99 | 
            +
            	// Use this for initialization
         | 
| 100 | 
            +
            	void Start () {
         | 
| 101 | 
            +
            		//Playerオブジェクトを取得
         | 
| 102 | 
            +
            		playerObj = GameObject.FindWithTag ("Player");
         | 
| 103 | 
            +
            	}
         | 
| 104 | 
            +
            	
         | 
| 105 | 
            +
            	// Update is called once per frame
         | 
| 106 | 
            +
            	void Update () {
         | 
| 107 | 
            +
            		//PlayerにアタッチされているPlayerParameterスクリプトを参照
         | 
| 108 | 
            +
            		PlayerParameter pp = playerObj.GetComponent<PlayerParameter> ();
         | 
| 109 | 
            +
            		//Shootingスクリプトを参照
         | 
| 110 | 
            +
            		Shooting st = GetComponent<Shooting> ();
         | 
| 111 | 
            +
            		//自分(Enemy)の座標を取得
         | 
| 112 | 
            +
            		mPos = transform.position;
         | 
| 113 | 
            +
            		//Playerの座標を取得
         | 
| 114 | 
            +
            		pPos = playerObj.transform.position;
         | 
| 115 | 
            +
            		//Playerの速度を格納
         | 
| 116 | 
            +
            		p_velocity = pp.GetVelocity();
         | 
| 117 | 
            +
            		//PlayerとEnemy間の距離を格納
         | 
| 118 | 
            +
            		//p_distance = Vector3.Distance(mPos, pPos);
         | 
| 119 | 
            +
            		//Playerの速度を送る
         | 
| 120 | 
            +
            		st.SetPlayerVelocity (p_velocity);
         | 
| 121 | 
            +
            		//Playerの予測移動位置を格納
         | 
| 122 | 
            +
            		forecastPos = pPos + pp.GetVelocityVector();
         | 
| 123 | 
            +
            		//targetを向く
         | 
| 124 | 
            +
            		transform.rotation = Quaternion.FromToRotation( Vector3.up,  forecastPos);
         | 
| 125 | 
            +
            		//PlayerのY座標を取得し、自分(Enemy)の座標に反映(=Playerと平行移動)
         | 
| 126 | 
            +
            		mPos.y = pPos.y;
         | 
| 127 | 
            +
            		transform.position = mPos;
         | 
| 128 | 
            +
            	}
         | 
| 129 | 
            +
            }
         | 
| 130 | 
            +
            ```
         | 
| 131 | 
            +
            ```c#
         | 
| 132 | 
            +
            using System.Collections;
         | 
| 133 | 
            +
            using System.Collections.Generic;
         | 
| 134 | 
            +
            using UnityEngine;
         | 
| 135 | 
            +
             | 
| 136 | 
            +
            //Rigidbody2Dコンポーネントを必須にする
         | 
| 137 | 
            +
            [RequireComponent(typeof(Rigidbody2D))]
         | 
| 138 | 
            +
            public class Shooting : MonoBehaviour {
         | 
| 139 | 
            +
             | 
| 140 | 
            +
            	//Bulletプレハブ
         | 
| 141 | 
            +
            	[SerializeField]
         | 
| 142 | 
            +
            	private GameObject muzzle;
         | 
| 143 | 
            +
            	//弾を撃つ間隔
         | 
| 144 | 
            +
            	[SerializeField]
         | 
| 145 | 
            +
            	private float shotDelay = 1f;
         | 
| 146 | 
            +
            	//弾の速度
         | 
| 147 | 
            +
            	[SerializeField]
         | 
| 148 | 
            +
            	private float bulletSpeed = 5f;
         | 
| 149 | 
            +
            	//Playerの速度
         | 
| 150 | 
            +
            	private float p_velocity = 0f;
         | 
| 151 | 
            +
             | 
| 152 | 
            +
            	// Update is called once per frame
         | 
| 153 | 
            +
            	void Update () {
         | 
| 154 | 
            +
             | 
| 155 | 
            +
            	}
         | 
| 156 | 
            +
             | 
| 157 | 
            +
            	// Startメソッドをコルーチンとして呼び出す
         | 
| 158 | 
            +
            	IEnumerator Start ()
         | 
| 159 | 
            +
            	{
         | 
| 160 | 
            +
            		while (true) {
         | 
| 161 | 
            +
            			//Debug.Log (GetBulletSpeed());
         | 
| 162 | 
            +
             | 
| 163 | 
            +
            			// 弾をプレイヤーと同じ位置/角度で作成
         | 
| 164 | 
            +
            			Instantiate (muzzle, transform.position, transform.rotation);
         | 
| 165 | 
            +
            			// 0.05秒待つ
         | 
| 166 | 
            +
            			yield return new WaitForSeconds (shotDelay);
         | 
| 167 | 
            +
            		}
         | 
| 168 | 
            +
            	}
         | 
| 169 | 
            +
             | 
| 170 | 
            +
            	//Enemyの速度を加算した弾の速度を返す
         | 
| 171 | 
            +
            	public float GetBulletSpeed(){
         | 
| 172 | 
            +
            		return bulletSpeed;
         | 
| 173 | 
            +
            	}
         | 
| 174 | 
            +
             | 
| 175 | 
            +
            	//Playerの速度をセットする関数
         | 
| 176 | 
            +
            	public void SetPlayerVelocity(float v){
         | 
| 177 | 
            +
            		p_velocity = v;
         | 
| 178 | 
            +
            	}
         | 
| 179 | 
            +
             | 
| 180 | 
            +
            	//Playerの速度を取得する関数
         | 
| 181 | 
            +
            	public float GetPlayerVelocity(){
         | 
| 182 | 
            +
            		return p_velocity;
         | 
| 183 | 
            +
            	}
         | 
| 184 | 
            +
            }
         | 
| 185 | 
            +
             | 
| 186 | 
            +
            ```
         | 
| 187 | 
            +
            ```c#
         | 
| 188 | 
            +
            using System.Collections;
         | 
| 189 | 
            +
            using System.Collections.Generic;
         | 
| 190 | 
            +
            using UnityEngine;
         | 
| 191 | 
            +
             | 
| 192 | 
            +
            //Rigidbody2Dコンポーネントを必須にする
         | 
| 193 | 
            +
            [RequireComponent(typeof(Rigidbody2D))]
         | 
| 194 | 
            +
            public class Bullet : MonoBehaviour {
         | 
| 195 | 
            +
             | 
| 196 | 
            +
            	//敵オブジェクト
         | 
| 197 | 
            +
            	private GameObject eneObj;
         | 
| 198 | 
            +
            	//Playerオブジェクト
         | 
| 199 | 
            +
            	private GameObject playerObj;
         | 
| 200 | 
            +
             | 
| 201 | 
            +
            	// Use this for initialization
         | 
| 202 | 
            +
            	void Start () {
         | 
| 203 | 
            +
            		//Playerオブジェクトを取得
         | 
| 204 | 
            +
            		playerObj = GameObject.FindWithTag ("Player");
         | 
| 205 | 
            +
            		//Enemyオブジェクトを取得
         | 
| 206 | 
            +
            		eneObj = GameObject.FindWithTag ("Enemy");
         | 
| 207 | 
            +
            		//EnemyオブジェクトのShootingスクリプトを参照
         | 
| 208 | 
            +
            		Shooting st = eneObj.GetComponent<Shooting> ();
         | 
| 209 | 
            +
            		//弾を発射する
         | 
| 210 | 
            +
            		GetComponent<Rigidbody2D> ().AddForce (transform.up.normalized * st.GetBulletSpeed(),ForceMode2D.Impulse);
         | 
| 211 | 
            +
            	}
         | 
| 212 | 
            +
            	
         | 
| 213 | 
            +
            	// Update is called once per frame
         | 
| 214 | 
            +
            	void Update () {
         | 
| 215 | 
            +
            		//PlayerにアタッチされているPlayerParameterスクリプトを参照
         | 
| 216 | 
            +
            		PlayerParameter pp = playerObj.GetComponent<PlayerParameter> ();
         | 
| 217 | 
            +
            		//Playerの移動ベクトルを加える
         | 
| 218 | 
            +
            		transform.position += pp.GetMoveVector ();
         | 
| 219 | 
            +
            	}
         | 
| 220 | 
            +
            }
         | 
| 221 | 
            +
            ```
         | 
