如何找到游戏对象所看方向的 2D 方向向量

How can I find a 2D direction vector of the direction gameobject is looking at

这里是重要的东西:

private RaycastHit2D[] FillRaycastArray()
    {

        List<RaycastHit2D> RaycastList = new List<RaycastHit2D>();

        Vector2 rayOrigin = new Vector2(animalTransform.position.x, animalTransform.position.y);

        animalTransform.rotation.ToAngleAxis(out float angle, out Vector3 axis);

        for (int i = 0; i < genes.sightQuality; i++)
        {
            float rightEyeAngle = angle - (genes.sightAngle / 2 + genes.sightWidth * i);
            float leftEyeAngle = angle + (genes.sightAngle / 2 + genes.sightWidth * i);

            Vector2 rightEyeDirectionVector = new Vector2((float)-Math.Sin(Mathf.Deg2Rad * rightEyeAngle), (float)Math.Cos(Mathf.Deg2Rad * rightEyeAngle));
            Vector2 leftEyeDirectionVector = new Vector2((float)-Math.Sin(Mathf.Deg2Rad * leftEyeAngle), (float)Math.Cos(Mathf.Deg2Rad * leftEyeAngle));

            Debug.DrawRay(rayOrigin, rightEyeDirectionVector, Color.red);
            Debug.DrawRay(rayOrigin, leftEyeDirectionVector, Color.red);

            int predatorLayer = 1 << 8;
            predatorLayer = ~predatorLayer;

            RaycastHit2D hitR = Physics2D.Raycast(rayOrigin, rightEyeDirectionVector, 20f, predatorLayer);
            RaycastHit2D hitL = Physics2D.Raycast(rayOrigin, leftEyeDirectionVector, 20f, predatorLayer);
            RaycastList.Add(hitR);
            RaycastList.Add(hitL);
        }

        RaycastHit2D[] raycastArray = RaycastList.ToArray();

        return raycastArray;
    }

我认为主要问题是将四元数转换为角度,然后将角度转换为方向向量以进行光线投射。我可以看到有什么不对劲,因为物体在旋转,光线也在旋转,但与物体的旋转方式无关。

使用Transform.TransformDirection[*见下文]将眼睛"frontward"方向从本地space转换为世界space , 然后使用 Quaternion.AngleAxis 创建左右旋转以找到视线方向。

Vector2 frontLocalDirection = new Vector2(0.7071f,0.7071f); 
// or whatever the local "front" direction is.

private RaycastHit2D[] FillRaycastArray()
{
    List<RaycastHit2D> RaycastList = new List<RaycastHit2D>();

    Vector2 rayOrigin = animalTransform.position;

    // will be converted to Vector2 later
    Vector3 frontWorldDirection = animalTransform.TransformDirection(
            frontLocalDirection);

    for (int i = 0; i < genes.sightQuality; i++)
    {
        float eyeAngleOffset = genes.sightAngle / 2 + genes.sightWidth * i;

        Vector2 rightEyeDirectionVector = Quaternion.AngleAxis(eyeAngleOffset, 
                Vector3.back) * frontWorldDirection;
        Vector2 leftEyeDirectionVector = Quaternion.AngleAxis(eyeAngleOffset, 
                Vector3.forward) * frontWorldDirection;

        Debug.DrawRay(rayOrigin, rightEyeDirectionVector, Color.red);
        Debug.DrawRay(rayOrigin, leftEyeDirectionVector, Color.red);

        int predatorLayer = 1 << 8;
        predatorLayer = ~predatorLayer;

        RaycastHit2D hitR = Physics2D.Raycast(rayOrigin, rightEyeDirectionVector, 20f,
                predatorLayer);
        RaycastHit2D hitL = Physics2D.Raycast(rayOrigin, leftEyeDirectionVector, 20f,
                predatorLayer);
        RaycastList.Add(hitR);
        RaycastList.Add(hitL);
    }

    RaycastHit2D[] raycastArray = RaycastList.ToArray();

    return raycastArray;
}

如果 frontLocalDirection 类似于 Vector2.up,您可以只使用 Transform 的便利字段之一,例如 transform.up 而不是使用 TransformDirection .例如:

private RaycastHit2D[] FillRaycastArray()
{
    List<RaycastHit2D> RaycastList = new List<RaycastHit2D>();

    Vector2 rayOrigin = animalTransform.position;

    // will be converted to Vector2 later
    Vector3 frontWorldDirection = animalTransform.up;

    for (int i = 0; i < genes.sightQuality; i++)
    {
        float eyeAngleOffset = genes.sightAngle / 2 + genes.sightWidth * i;

        Vector2 rightEyeDirectionVector = Quaternion.AngleAxis(eyeAngleOffset,
                Vector3.back) * frontWorldDirection;
        Vector2 leftEyeDirectionVector = Quaternion.AngleAxis(eyeAngleOffset, 
                Vector3.forward) * frontWorldDirection;

        Debug.DrawRay(rayOrigin, rightEyeDirectionVector, Color.red);
        Debug.DrawRay(rayOrigin, leftEyeDirectionVector, Color.red);

        int predatorLayer = 1 << 8;
        predatorLayer = ~predatorLayer;

        RaycastHit2D hitR = Physics2D.Raycast(rayOrigin, rightEyeDirectionVector, 20f,
                predatorLayer);
        RaycastHit2D hitL = Physics2D.Raycast(rayOrigin, leftEyeDirectionVector, 20f,
                predatorLayer);
        RaycastList.Add(hitR);
        RaycastList.Add(hitL);
    }

    RaycastHit2D[] raycastArray = RaycastList.ToArray();

    return raycastArray;
}