Exemplo n.º 1
0
 public void Warp(Vector2 v)
 {
     // try to warp, set this agent's position immediately if it worked, so
     // that Update doesn't cause issues when trying to move the rigidbody to
     // a far away position etc.
     if (agent.Warp(NavMeshUtils2D.ProjectPointTo3D(v)))
     {
         transform.position = v;
     }
 }
Exemplo n.º 2
0
    public bool CalculatePath(Vector2 targetPosition, NavMeshPath2D path)
    {
        var temp = new NavMeshPath();

        if (agent.CalculatePath(NavMeshUtils2D.ProjectPointTo3D(targetPosition), temp))
        {
            // convert 3D to 2D
            path.corners = temp.corners.Select(NavMeshUtils2D.ProjectTo2D).ToArray();
            path.status  = temp.status;
            return(true);
        }
        return(false);
    }
Exemplo n.º 3
0
    // based on: https://docs.unity3d.com/ScriptReference/AI.NavMesh.SamplePosition.html
    public static bool SamplePosition(Vector2 sourcePosition, out NavMeshHit2D hit, float maxDistance, int areaMask)
    {
        NavMeshHit hit3D;

        if (NavMesh.SamplePosition(NavMeshUtils2D.ProjectPointTo3D(sourcePosition), out hit3D, maxDistance, areaMask))
        {
            hit = new NavMeshHit2D {
                position = NavMeshUtils2D.ProjectTo2D(hit3D.position),
                normal   = NavMeshUtils2D.ProjectTo2D(hit3D.normal),
                distance = hit3D.distance,
                mask     = hit3D.mask,
                hit      = hit3D.hit
            };
            return(true);
        }
        hit = new NavMeshHit2D();
        return(false);
    }
Exemplo n.º 4
0
    // based on: https://docs.unity3d.com/ScriptReference/AI.NavMesh.Raycast.html
    public static bool Raycast(Vector2 sourcePosition, Vector2 targetPosition, out NavMeshHit2D hit, int areaMask)
    {
        NavMeshHit hit3D;

        if (NavMesh.Raycast(NavMeshUtils2D.ProjectPointTo3D(sourcePosition),
                            NavMeshUtils2D.ProjectPointTo3D(targetPosition),
                            out hit3D,
                            areaMask))
        {
            hit = new NavMeshHit2D {
                position = NavMeshUtils2D.ProjectTo2D(hit3D.position),
                normal   = NavMeshUtils2D.ProjectTo2D(hit3D.normal),
                distance = hit3D.distance,
                mask     = hit3D.mask,
                hit      = hit3D.hit
            };
            return(true);
        }
        hit = new NavMeshHit2D();
        return(false);
    }