从2点之间的方向获取值-1至1

时间:2019-07-12 23:13:43

标签: c# unity3d physics

我希望控制AI汽车并将其驶向玩家汽车。我已经计算出了一条通往玩家汽车的路径,但是我很难确定AI汽车沿着该路径行驶所要使用的转向角。

汽车的转向比为-1:1。

这是我尝试过的方法,但汽车只是绕圈转圈。

void UpdatePath()
{
    NavMesh.CalculatePath(transform.position, _Target.position, 1 << 0, path);
    DrawPath();

    Vector3 relativePos = path.corners[0] - transform.position;
    Quaternion targetRotation = Quaternion.LookRotation(relativePos);

    float y = transform.eulerAngles.y;
    float DeltaAngle = Mathf.DeltaAngle(y, targetRotation.y);


   float delta = Mathf.Clamp(DeltaAngle, -1, 1);

这里有什么想法吗?

谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用Min-Max归一化。

public float MinMaxNormalization(float point, float MaxPoint, float MinPoint){
        float value = (point - MinPoint) / (MaxPoint - MinPoint); // the value is between 0 and 1
        value *= 2; // now the value is between 0 and 2
        value -= 1; // now the value is between -1 and 1
        return value;
    }

我假设MaxPoint = 90.0f和MinPoint = -90.0f,所以

float MaxPoint = 90.0f;
float MinPoint = -90.0f;
float delta;
delta = MinMaxNormalization(90, MaxPoint , MinPoint); //delta will be 1
delta = MinMaxNormalization(0, MaxPoint , MinPoint); // delta will bi 0
delta = MinMaxNormalization(-90, MaxPoint , MinPoint); // delta will be -1
delta = MinMaxNormalization(45, MaxPoint , MinPoint); // delta will be 0.5
delta = MinMaxNormalization(-45, MaxPoint , MinPoint); // delta will be -0.5