找到一个角度将射弹发射到特定位置

问题描述

因此,达妮在他的新视频中->“做游戏,但我只有3天”(https://youtu.be/S7Dl6ATRK2M)变成了一个拥有弓箭的敌人(5:39)。我试图重新创建它,但没有运气...我也找不到他使用的网站...今天我找到了这个https://physics.stackexchange.com/questions/56265/how-to-get-the-angle-needed-for-a-projectile-to-pass-through-a-given-point-for-t。它工作得很好,但是如果目标离得很远,而且还不够准确,仍然会出现问题。到目前为止的代码是

float CalculateAngle()
    {
        float gravity = Physics.gravity.magnitude;
        float deltaX = targetPositionMod.x - currentPosition.x;
        float deltaY = targetPositionMod.y - currentPosition.y;
        float RHSFirstPart = (velocity * velocity) / (gravity * deltaX);
        float RHSSecondPart = Mathf.Sqrt(
                ((velocity * velocity) * ((velocity * velocity) - (2 * gravity * deltaY)) 
                                / (gravity * gravity * deltaX * deltaX))
                                                - 1);
        float tanθ = RHSFirstPart - RHSSecondPart;
        float angle = Mathf.Atan2(tanθ,1) * Mathf.Rad2Deg;
        if (angle < 0) return angle;
        return -angle;
    }

-angle是因为当x旋转为负(统一)时,前向轴开始指向上。也许这不是按预期方式工作的原因是我对这种物理并不擅长(部分原因是我只有14岁)。也许问题出在代码中,也许是公式。任何帮助表示赞赏。

谢谢...

编辑: 弓箭手类是:

using UnityEngine;
using System;
public class Archer : MonoBehaviour
{
    [SerializeField] float velocity = default;
    [SerializeField] Transform target = default;
    [SerializeField] GameObject arrowPrefab = default;
    [SerializeField] float coolDown = default;
    Vector3 targetPositionMod;
    Vector3 currentPosition;
    Vector3 targetPosition;
    float countDown = 0f;

    void Start()
    {
        countDown = coolDown;
        UpdateVariables();
    }

    void Update()
    {
        UpdateVariables();
        SetAngle();
        ShootBullet();
    }
    
    void UpdateVariables()
    {
        currentPosition = transform.position;
        targetPositionMod = Mod(target.position);
        targetPosition = target.position;
        targetPosition.x /= 10;
        targetPosition.y /= 10;
        targetPosition.z /= 10;
        countDown -= Time.deltaTime;
    }

    void SetAngle()
    {
        Vector3 direction = targetPosition - currentPosition;
        Quaternion lookRotation = Quaternion.LookRotation(direction);
        Vector3 rotation = lookRotation.eulerAngles;
        rotation.x = (float) CalculateAngle();
        transform.rotation = Quaternion.Euler(rotation.x,rotation.y,0f);
    }

    void ShootBullet()
    {
        if (!(countDown <= 0f)) return;
        countDown = coolDown;
        GameObject arrow = Instantiate(arrowPrefab,transform.position,transform.rotation);
        Rigidbody Rigidbody = arrow.GetComponent<Rigidbody>();
        Rigidbody.AddForce(transform.forward * velocity,ForceMode.Impulse);
    }

    double CalculateAngle()
    {
        double gravity = Physics.gravity.magnitude;
        double deltaX = targetPositionMod.x - currentPosition.x;
        double deltaY = targetPositionMod.y - currentPosition.y;
        double RHSFirstPart = (velocity * velocity) / (gravity * deltaX);
        double RHSSecondPart = Math.Sqrt(
                (((velocity * velocity) * ((velocity * velocity) - (2 * gravity * deltaY)) 
                                / (gravity * gravity * deltaX * deltaX))
                                                - 1));
        double tanθ = RHSFirstPart - RHSSecondPart;
        double angle = Math.Atan2(tanθ,1) * Mathf.Rad2Deg;
        if (angle < 0) return angle;
        return -angle;
    }
    
    Vector3 Mod(Vector3 Vec)
    {
        if (Vec.x < 0) Vec.x -= 2 * Vec.x;
        if (Vec.y < 0) Vec.y -= 2 * Vec.y;
        if (Vec.z < 0) Vec.z -= 2 * Vec.z;
        Vec.x /= 10;
        Vec.y /= 10;
        Vec.z /= 10;
        return Vec;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)