【问题标题】:Unity - Projectile Motion, find the angle needed to hit coordinates x,yUnity - 弹丸运动,找到击中坐标 x,y 所需的角度
【发布时间】:2020-03-25 19:38:26
【问题描述】:

围绕同一主题提出了很多问题,但似乎没有什么对我有用。

问题很简单,一个玩家和一个敌人在 x,y 平面上。我想以一个计算好的角度发射我的弹丸,这样弹丸就会在它的坐标处击中敌人。

我都试过了

Angle of ReachAngle required to hit x,y

这两种实现最终都为我做同样的事情; Shooting but not hitting the target in this manner

任何帮助或指点将不胜感激!谢谢

代码如下:

public Rigidbody projectile;
public float projectileSpeed;
public float Firerate = 9f;
private float nextfire;

private GameObject enemy;
private float gravity = Physics.gravity.y;
private Vector3 directionalVector;

// Start is called before the first frame update
void Start()
{
    enemy = GameObject.FindGameObjectWithTag("enemy");

}


void Update()
{
    directionalVector = enemy.transform.position - transform.position;

}
void FixedUpdate()
{

    nextfire = Time.time + (1 / Firerate);

    float projectileSpeed2 = projectileSpeed * projectileSpeed;
    float projectileSpeed4 = projectileSpeed2 * projectileSpeed2;
    float x = enemy.transform.position.x;
    float y = enemy.transform.position.y;
    float x2 = x * x;



    float theta = Mathf.Atan(projectileSpeed2-Mathf.Sqrt(projectileSpeed4-gravity*(gravity*x2+2*y*projectileSpeed2))/gravity*x);
    print(theta);

    Vector3 releaseVector = (Quaternion.AngleAxis(theta, Vector3.up) * directionalVector).normalized;

    Debug.DrawRay(transform.position, releaseVector, Color.red,0.5f);
    Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
    instantiatedProjectile.velocity = releaseVector * projectileSpeed;
}

}

【问题讨论】:

    标签: unity3d projectile


    【解决方案1】:

    为什么不避免找角度的问题,只是根据第一次看到敌人的方向移动子弹。

    (target.transform.position - transform.position).normalized;

    它会返回一个指向目标的向量方向。

    当弹丸移动时,只需按照这个方向移动即可。
    计算角度无需头疼:)

    编辑

    我之前做了一个函数来将角度“转换”为方向:

    protected Vector2 DetermineBulletMoveDirection(float shootingAngle) {
        // Determine the direction of the bullet travel on the x and y axis.
        float bulletDirectionX = transform.position.x + Mathf.Sin((shootingAngle * Mathf.PI) / 180);
        float bulletDirectionY = transform.position.y + Mathf.Cos((shootingAngle * Mathf.PI) / 180);
    
        // Determines the direction this bullet should be moving.
        Vector2 bulletDirection = new Vector2(bulletDirectionX, bulletDirectionY);
        return (bulletDirection - (Vector2)transform.position).normalized;
    }
    

    它接收一个角度,并根据射手当前所处的位置将其转换为方向。
    角度应从Vector.down 开始,顺时针旋转。

    下一个问题是找出你和敌人之间的角度。
    这是我能想到的最简单的解决方案,先上一张图:

    注意到您可以在此使用TOACAHSOH

    所以您所要做的就是“虚拟地”将射手的 Y 轴与原点对齐。
    (也将运动应用于射手!)

    对射手做同样的事情,但这次是在 x 轴上。

    您将能够实现具有 90 度三角形的状态。
    从那里开始,您可以计算从Vector.down 旋转到敌人的角度。

    只需确保将两个对象都移回其初始位置即可。

    【讨论】:

    • 哦,那太容易了 :) 我想弄清楚角度的问题。
    • @vtkr 编辑了我的答案,希望对您有所帮助。
    • 感谢您的输入,除非我完全误解了您的观点,否则我相信我所处的情况有点不同。所以我不是从上往下看角度,而是从侧面看;像横向卷轴。游戏。我觉得我真的很接近解决方案,因为确实发生了角度射击,但似乎在弹丸运动和敌人的位置之间总是存在某种偏移
    【解决方案2】:

    经过一段时间的斗争,我找到了解决方案。

    最后我最终使用了Angle of Reach。第二个错误是 Mathf.Atan 返回弧度而不是度数,而 Quantion.AngleAxis 接受角度。第三个也是最后一个是 Unity 使用左手坐标系,而不是我习惯的通常右手坐标系。

    这是最后一段代码:

    public class TargetAndShoot : MonoBehaviour
    {
        public Rigidbody projectile;
        public float projectileSpeed;
        public float firerate;
        private float nextfire;
    
        private GameObject enemy;
        private float gravity = Physics.gravity.y;
    
    
        // Start is called before the first frame update
        void Start()
        {
            enemy = GameObject.FindGameObjectWithTag("enemy");
    
        }
    
    
        void Update()
        {
            if (Time.time >= nextfire)
            {
                nextfire = Time.time + (1 / firerate);
                float distance = enemy.transform.position.x - transform.position.x;
                Vector3 directionalVector = enemy.transform.position - transform.position;
    
                float v2 = projectileSpeed * projectileSpeed;
                float v4 = v2 * v2;
    
                float x = enemy.transform.position.x;
                float x2 = x * x;
                float y = enemy.transform.position.y;
    
                float theta = 0.5f*Mathf.Asin((gravity * distance) / (projectileSpeed * projectileSpeed));
                Vector3 releaseVector = (Quaternion.AngleAxis(theta * Mathf.Rad2Deg, -Vector3.forward) * directionalVector).normalized;
                Debug.DrawRay(transform.position, releaseVector*5, Color.cyan, 0.5f);
    
                Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
                instantiatedProjectile.velocity = releaseVector * projectileSpeed;
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多