Пример #1
0
    private Vector3 returnSteeringDirection(Vector3 targetPos)
    {
        // Clear List
        rays.Clear();

        float angle = 0;

        // Get new Path
        getPath(targetPos);

        // Lets get desired direction
        Vector3 nextCornerDirection = (navMeshPath.corners[1] - transform.position).normalized;

        // Get all rays
        for (int i = 0; i < rayAmount; i++)
        {
            // Create rayDot
            rayDot r = new rayDot();

            // Rotate Vector
            angle = 360 / rayAmount * i;
            Vector3 avoidanceDirection = (Quaternion.AngleAxis(angle, Vector3.up) * transform.forward).normalized;

            // Init ray + yAxis Offset
            Ray ray = new Ray(transform.position + new Vector3(0, 0.3f, 0), avoidanceDirection);

            // Store ray in Array
            r.ray = ray;

            // FYI: Dot Product Vectors need to be normalized
            r.dotValue = Vector3.Dot(nextCornerDirection, avoidanceDirection);

            // Raycast
            Physics.Raycast(ray, out r.hitInfo, neighbourRadius, -1, QueryTriggerInteraction.UseGlobal);

            // Substract normalized distance to dot product
            r.dotValue -= r.hitInfo.distance / neighbourRadius;

            // Debug ray
            Debug.DrawRay(r.ray.origin, r.ray.direction * r.dotValue, Color.green);

            // add r to list
            rays.Add(r);
        }

        // Find Ray with the highest Dot value
        rayDot mainDirection = returnHighestDotValue(rays.ToArray());

        // Debug Ray
        Debug.DrawRay(mainDirection.ray.origin, mainDirection.ray.direction * mainDirection.dotValue, Color.red);

        return(mainDirection.ray.direction);
    }
Пример #2
0
    private rayDot returnHighestDotValue(rayDot[] rays)
    {
        rayDot rd  = new rayDot();
        float  dot = 0;

        foreach (rayDot r in rays)
        {
            if (r.dotValue >= dot)
            {
                dot = r.dotValue;
                rd  = r;
            }
        }
        return(rd);
    }
Пример #3
0
    private List <rayDot> GetRayHitObjects()
    {
        List <rayDot> rd = new List <rayDot>();

        float angle = 0;

        // Vector3 nextCornerDirection = (navMeshPath.corners[1] - transform.position).normalized;

        for (int i = 0; i < rayAmount; i++)
        {
            // Create rayDot
            rayDot r = new rayDot();

            // Rotate Vector
            angle = 360 / rayAmount * i;
            Vector3 avoidanceDirection = (Quaternion.AngleAxis(angle, Vector3.up) * transform.forward).normalized;

            // Init ray + yAxis Offset
            Ray ray = new Ray(transform.position + new Vector3(0, 0.3f, 0), avoidanceDirection);

            // Store ray in Array
            r.ray = ray;

            // FYI: Dot Product Vectors need to be normalized
            // r.dotValue = Vector3.Dot(nextCornerDirection, avoidanceDirection);

            // Raycast
            Physics.Raycast(ray, out r.hitInfo, neighbourRadius, -1, QueryTriggerInteraction.UseGlobal);

            // Substract normalized distance to dot product
            r.dotValue -= r.hitInfo.distance / neighbourRadius;

            // Debug ray
            Debug.DrawRay(r.ray.origin, r.ray.direction * r.dotValue, Color.green);

            // add r to list
            rd.Add(r);
        }

        return(rd);
    }