Find nearest waypoint to target in unity 3d

I’m developing game like Hitman Go , and I need to find which waypoints are closest to my target(specific waypoint ) when enemy has alerted by rock or sound and etc.

I set some point for enemy and My enemy patrolling between waypoints ( 8 → 6 → 1-> 2-> 3-> 4-> 5 ) then reveres his path. so, when I throw rock in waypoint number 18, I need to move enemy to this waypoint, but with closest way. imaging enemy can be any this waypoint when he get aleret ( 8,6,1,2,3,4,5 points).

Note 1: The distance between two points is same.

Note2: My game is in 3d not 2d.

I use this code to move my enemy step by step ( turn base ).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WaypointController : MonoBehaviour
{
    public List<Transform> waypoints = new List<Transform>();
    private Transform targetWaypoint;
    private int targetWaypointIndex = 0;
    private float minDistance = 0.1f;
    private int lastWaypointIndex;

    public bool reversePath;

    // what easetype to use for iTweening
    public iTween.EaseType easeType = iTween.EaseType.easeInOutExpo;

    // how fast we move
    public float moveSpeed = 1.5f;

    // time to rotate to face destination
    public float rotateTime = 0.5f;

    // delay to use before any call to iTween
    public float iTweenDelay = 0f;

    // Use this for initialization
    void Start()
    {
        lastWaypointIndex = waypoints.Count - 1;
        targetWaypoint = waypoints[targetWaypointIndex];
    }

    public void EnemyTurn()
    {
        float distance = Vector3.Distance(transform.position, targetWaypoint.position);
        CheckDistanceToWaypoint(distance);

        // move toward the destinationPos using the easeType and moveSpeed variables
        iTween.MoveTo(gameObject, iTween.Hash(
            "x", targetWaypoint.position.x,
            "y", targetWaypoint.position.y,
            "z", targetWaypoint.position.z,
            "delay", iTweenDelay,
            "easetype", easeType,
            "speed", moveSpeed
        ));
    }

    void CheckDistanceToWaypoint(float currentDistance)
    {
        if (currentDistance <= minDistance)
        {
            targetWaypointIndex++;
            UpdateTargetWaypoint();
        }
    }

    void UpdateTargetWaypoint()
    {
        if (targetWaypointIndex > lastWaypointIndex)
        {
            if (reversePath)
            {
                waypoints.Reverse();
            }
       
            targetWaypointIndex = 1;
        }
        targetWaypoint = waypoints[targetWaypointIndex];
    }
}

You’ll need a graph representing your world and then a pathfinding solution like A* to calculate the shortest path.

Hi @GroZZleR , thanks. I try A* graph point example and it seems good. but do you know ho can I stop enemy after reach a poin? link turn base games? I mean enemy and player must move step by step.

I’d use a coroutine and move them from node to node, then stop executing it when they’ve arrived at their destination.