Get it on Google Play


Wm뮤 :: 유니티 NavMesh

블로그 이미지
가끔 그림그리거나 3D모델링하거나
취미로 로봇만드는
퇴직한 전자과 게임프로그래머
2020.3.48f1 , 2022.3.6f1 주로 사용
모카쨩
@Ahzkwid

Recent Comment

Archive


2023. 7. 16. 20:00 Unity

 

 

 

길이 가져오기

도달했나 검사할때 씀

remainingDistance가 잘 안작동 해서 만듦

public static float GetPathLength(NavMeshPath path)
{
    float length = 0f;

    if ((path.status != NavMeshPathStatus.PathInvalid) && (path.corners!=null))
    {
        for (int i = 1; i < path.corners.Length; ++i)
        {
            length += Vector3.Distance(path.corners[i - 1], path.corners[i]);
        }
    }
    else
    {
        length = 99999f;
    }

    return length;
}

 

 

 

디버깅용

V1

if (Application.platform==RuntimePlatform.WindowsEditor)
{
    if ((path!=null)
        && (path.status != NavMeshPathStatus.PathInvalid) 
        && (path.corners != null))
    {
        for (int i = 0; i < path.corners.Length - 1; i++)
        {
            Debug.DrawLine(path.corners[i], path.corners[i + 1], Color.green);
        }
    }
}

 

V2

#if UNITY_EDITOR
void OnDrawGizmos()
{
    var agent = GetComponent<NavMeshAgent>();
    if (agent == null)
    {
        UnityEditor.Handles.Label(transform.position, "agent == null");
        return;
    }
    //var path = agent.path;
    if (path == null)
    {
        UnityEditor.Handles.Label(transform.position, "path == null");
        return;
    }
    if (path.status == NavMeshPathStatus.PathInvalid)
    {
        UnityEditor.Handles.Label(transform.position, "path.status == NavMeshPathStatus.PathInvalid");
        return;
    }
    if (path.corners == null)
    {
        UnityEditor.Handles.Label(transform.position, "path.corners == null");
        return;
    }
    if (path.corners.Length < 2)
    {
        UnityEditor.Handles.Label(transform.position, "path.corners.Length < 2");
        return;
    }
    UnityEditor.Handles.Label(transform.position, $"path.corners.Length : {path.corners.Length}\npath.corners[1] : {path.corners[1]}");
    for (int i = 0; i < path.corners.Length - 1; i++)
    {
        Debug.DrawLine(path.corners[i], path.corners[i + 1], Color.green);
    }
    Gizmos.DrawWireSphere(transform.position, agent.stoppingDistance);
}
#endif

 

 

 

 

목적지 설정


public void SetDestination(Transform target)
{
    var agent = GetComponent<NavMeshAgent>();
    if ((target != null) && (agent != null))
    {
        agent.SetDestination(target.position);
    }
    else
    {
        agent.ResetPath();
    }
}

 

 

 

패스를 WASD로 변경



/// <summary>
/// x는 horizon (-1 ~ 1)
/// y는 height (Mathf.NegativeInfinity ~ Mathf.Infinity)
/// z는 vertical (-1 ~ 1)
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
Vector3 Path2WASD(NavMeshPath path)
{
    if (path == null)
    {
        return Vector3.zero;
    }
    if (path.status == NavMeshPathStatus.PathInvalid)
    {
        return Vector3.zero;
    }
    if (path.corners == null)
    {
        return Vector3.zero;
    }
    var agent = GetComponent<NavMeshAgent>();

    var stoppingDistance = 0.1f;
    if (agent!=null)
    {
        stoppingDistance = agent.stoppingDistance;
    }

    if (stoppingDistance > GetPathLength(path))
    {
        return Vector3.zero;
    }



    if (path.corners.Length<2)
    {
        return Vector3.zero;
    }

    var movePos = path.corners[1];


    if (path.corners.Length >= 3)
    {
        if (Vector3.Distance(path.corners[0], path.corners[1]) < 0.1f)
        {
            movePos = path.corners[2];
        }
    }
    Debug.DrawLine(transform.position + Vector3.up * 0.1f, movePos + Vector3.up * 0.1f, Color.blue);

    var relativePosition = Quaternion.Inverse(transform.rotation) * (movePos - transform.position);
    var relativePosition2D = new Vector2(relativePosition.x, relativePosition.z).normalized;
    relativePosition = new Vector3(relativePosition2D.x, relativePosition.y, relativePosition2D.y);

    Debug.DrawLine(transform.position + Vector3.up * 0.2f, transform.position + transform.rotation*relativePosition + Vector3.up * 0.2f, Color.red);
    return relativePosition;
}

 

'Unity' 카테고리의 다른 글

유니티 Final IK  (0) 2023.08.11
유니티 애니메이션 관련  (0) 2023.04.03
구글플레이 앱 이전  (0) 2023.03.06
posted by 모카쨩

  • total
  • today
  • yesterday

Recent Post

저사양 유저용 블로그 진입