0

私は発射体のポイントを地上のある高さに持っており、敵(右から左へ、そして水平軸の射撃点に向かって移動している)は地面にあります.私が持っている発射体の動きは、角度をつけて下向きの動きで直接発射します敵ですが、発射体が最初に上向きになり、次に敵を撃ちたいと思います。

以下にスナップショットを添付しました。

これが私のコードです:

using UnityEngine;
using System.Collections;

public class bullet : MonoBehaviour {
    public Transform target;
    private Transform mytransform;
    public GameObject bulletprefab;
    public GameObject enemyprefab;
    private gamenemy e;

    public float firingAngle = 20;
    public float gravity = 9.8f;

    void Awake()
    {
        mytransform = transform;      
    }
    // Use this for initialization
    void Start () {
    mytransform.LookAt(target);
        StartCoroutine (project ());
        //follow();
    }
    IEnumerator project()
    {    yield return new WaitForSeconds(0.25f);
        float target_Distance = Vector3.Distance(mytransform.position * target_Distance , target.position);
        // Calculate the velocity needed to throw the object to the target at specified angle.
        float projectile_Velocity = target_Distance / (Mathf.Sin(2 * target_Distance * Mathf.Deg2Rad) / gravity);


        // Extract the X  Y componenent of the velocity
        float Vx = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(target_Distance * Mathf.Deg2Rad);
        float Vy = Mathf.Sqrt(projectile_Velocity) * Mathf.Sin( 1/target_Distance * Mathf.Deg2Rad);



        // Calculate flight time.
        float flightDuration = target_Distance / Vx;

        // Rotate projectile to face the target.
        mytransform.rotation = Quaternion.LookRotation(target.position - mytransform.position);

        float elapse_time = 0;

        while (elapse_time < flightDuration)
        {
            mytransform.Translate(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime);

            elapse_time += Time.deltaTime;

            yield return null;
        }
    }



    Snapshot of how it is with given code:

ここに画像の説明を入力

   This is how I want it to be:

ここに画像の説明を入力

4

1 に答える 1

2

コードで動きを計算するのではなく、発射体で Rigidbody を使用することを検討してください。次に、発砲の時点で、最初の弾道 (つまり、砲身が向いている方向) に対して力を加え、弾丸に重力を作用させます。

リジッドボディの詳細

于 2014-07-28T15:09:29.693 に答える