Как найти двухмерный вектор направления, на который смотрит игровой объект? - PullRequest
0 голосов
/ 10 января 2020

Вот важные вещи:

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;
    }

Я думаю, что основная проблема заключается в преобразовании кватернионов в углы, а затем углов в вектор направления для лучевого вещания. Я вижу, что что-то не так, потому что объект вращается, а лучи вращаются, но не связаны с объектами.

1 Ответ

0 голосов
/ 10 января 2020

Используйте Transform.TransformDirection [* см. Ниже] , чтобы преобразовать направление "вперед" глаз из локального пространства в мировое пространство, затем используйте 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;
}
...