考虑初始对象的前向角,找到2个对象之间的航向角

好吧,所以我已经坚持了好多年了。我正在研究一种将坦克导航到定义为Vector3的航路点的AI。坦克的位置也定义为Vector3,这两个位置的Y位置都设为0,以忽略地形高程,坦克的当前旋转也是Vector3,尽管仅需要Y旋转,因为i' m有效地将3d位置投影到2d导航网格上。

AI将-1到1之间的任意位置传递到坦克的控件中,然后该控件处理物理操作。因此,我需要以某种方式计算相对于当前油箱到航路点位置的航向角的正或负角,然后将旋转值发送给控件。目前,我无法正常工作,我感觉我已经尝试了一切。

这是我目前的代码,根本不起作用,大约是第20版:

void driveToTarget()
{

    Vector3 target0 = drivetarget;
    target0.y = 0;
    GameObject current0Obj = new GameObject();
    Vector3 current0 = this.transform.position;
    current0.y = 0;
    print(current0);
    print(target0);
    Vector3 current0Angle = this.transform.eulerAngles;
    print(current0Angle.y);
    current0Angle.x = 0;
    current0Angle.z = 0;

    Vector3 heading = target0 - current0;

    Quaternion headingAngle = Quaternion.LookRotation(heading);
    print("headingAngle" + headingAngle);
    print("heading direction,allegidly: " + Quaternion.Euler(heading).ToEulerAngles());
    Quaternion distanceToGo = Quaternion.Lerp(Quaternion.Euler(current0Angle),headingAngle,0.01f);

    float angle = Vector3.SignedAngle(current0,target0,Vector3.up);
    float difference = Mathf.Abs(angle - current0Angle.y);



    print("heading angle " + angle);
    if (current0 != drivetarget)
    {
        steeringVal = Mathf.Abs(1.5f-(1f/Mathf.Abs(distanceToGo.y))) * -Mathf.Sign(distanceToGo.y); ;
        throttleVal = 0f;
    } else
    {
        throttleVal = 0;
    }

}

-编辑-

因此,我已经部分解决了它,现在又遇到了另一个问题,我已经设法让战车能够检测自身与航路点之间的角度,但不是朝着航路点(即右侧)坦克朝向它定向,因此它绕着航路点旋转。我实际上知道为什么会这样,因为从技术上讲,坦克的前向矢量是正确的矢量,因为统一的愚蠢轴破坏了我的搅拌机进口,无论如何,这是更新的代码:

void driveToTarget()
{

    Vector3 target0 = drivetarget;
    target0.y = 0;
    Vector3 current0 = this.transform.position;
    current0.y = 0;
    print("Current: " + current0);
    print("Target: " + target0);
    Vector3 current0Angle = this.transform.rotation.eulerAngles;
    print("Curret rotation:" + current0Angle.y);
    current0Angle.x = 0;
    current0Angle.z = 0;

    Vector3 heading = target0 - current0;

    Quaternion headingAngle = Quaternion.LookRotation(heading);
    print("heading angle: " + headingAngle.ToEuler());

    float distanceToGo = (current0Angle.y) - headingAngle.eulerAngles.y;
    print("DistanceToGo: " + distanceToGo);




    if (current0 != drivetarget)
    {

            steeringVal = 1 * -Mathf.Sign(distanceToGo);


        throttleVal = 0f;
    } else
    {
        throttleVal = 0;
    }
    Debug.DrawRay(current0,heading,Color.red,1);
    Debug.DrawRay(current0,this.transform.up,1);

}
wqtan5566 回答:考虑初始对象的前向角,找到2个对象之间的航向角

我不确定您的代码是如何设置的或指导的工作方式。您可能想研究使用Unity NavMeshAgent简化此过程。

不管这是我写下的一些代码,它都会接收一个目标并将一个对象旋转到该目标。从那里开始,您所要做的就是将对象向前移动。

 Vector3 nextDestination = //destination;
        Vector3 direction = nextDestination - transform.position;
        direction = new Vector3(direction.x,direction.z);
        var newRotation = Quaternion.LookRotation(direction);
        var finalRotation = Quaternion.Slerp(transform.rotation,newRotation,Time.deltaTime); //smoothes out rotation
        transform.rotation = finalRotation;

很抱歉,如果这不是您所需要的。您是否能够从打印语句中找出代码的哪一部分行为异常?

本文链接:https://www.f2er.com/3106201.html

大家都在问