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

与其在代码中计算运动,不如在射弹上使用刚体。然后在射击点,相对于初始轨迹(即枪管朝向的方向)施加一个力,并且重力也会影响你的子弹。

更多关于刚体

于 2014-07-28T15:09:29.693 回答