Exemplo n.º 1
0
    public static bool SetDestination2D(this NavMeshAgent nmAgent, Vector2 destination, bool startInstantly = false)
    {
        if (!nmAgent.isActiveAndEnabled)
        {
            return(false);
        }
        float z             = nmAgent.GetAgentZ();
        var   destination3D = new Vector3(destination.x, destination.y, z);

        if (startInstantly)
        {
            var path = new NavMeshPath();
            if (nmAgent.CalculatePath(destination, path))
            {
                nmAgent.SetPath2D(path);
            }
        }

        /*NavMeshHit hit;
         * if(NavMesh.SamplePosition(destination3D, out hit, 1f, NavMesh.AllAreas)) {
         *  destination3D = hit.position;
         * }*/
#if UNITY_EDITOR
        var helper = nmAgent.GetComponent <NavAgentHelper>();
        if (helper != null)
        {
            helper.lastDestination2D = destination3D;
        }
#endif
        return(nmAgent.SetDestination(destination3D));
    }
Exemplo n.º 2
0
 public static bool GetTraversablePosition(this NavMeshAgent nmAgent, Vector3 originalPosition, out Vector2 result, float maxDistance)
 {
     originalPosition.z = nmAgent.GetAgentZ();
     if (!NavMesh.SamplePosition(originalPosition, out var hit, maxDistance, traversableMask.Value))
     {
         result = default;
         return(false);
     }
     result = hit.position;
     return(true);
 }
Exemplo n.º 3
0
    public static bool SamplePosition(this NavMeshAgent nmAgent, Vector3 position, float maxDistance, out Vector2 result)
    {
        position.z = nmAgent.GetAgentZ();
        NavMeshHit hit;

        if (!NavMesh.SamplePosition(position, out hit, maxDistance, NavMesh.AllAreas))
        {
            result = position;
            return(false);
        }
        result = hit.position;
        return(true);
    }
Exemplo n.º 4
0
 public static bool CanReach(this NavMeshAgent nmAgent, Vector3 position, out NavMeshPath path)
 {
     path       = new NavMeshPath();
     position.z = nmAgent.GetAgentZ();
     return(nmAgent.CalculatePath(position, path) && path.status == NavMeshPathStatus.PathComplete);
 }
Exemplo n.º 5
0
 public static bool Raycast2D(this NavMeshAgent nmAgent, Vector3 targetPosition, out NavMeshHit hit)
 {
     targetPosition.z = nmAgent.GetAgentZ();
     return(nmAgent.Raycast(targetPosition, out hit));
 }