private void AddNewNode(CardinalDirectionNode baseNode, Vector3 newPos)
    {
        CardinalDirectionNode newNode = Instantiate(baseNode, baseNode.transform.position + newPos, Quaternion.identity) as CardinalDirectionNode;

        int nodeNum = 0;
        while(GameObject.Find("Node " + nodeNum)) nodeNum++;
        newNode.name = "Node " + nodeNum;
        newNode.northNode = null;
        newNode.southNode = null;
        newNode.westNode = null;
        newNode.eastNode = null;

        newNode.transform.parent = baseNode.transform.parent;
        if(newPos == Vector3.up)
        {
                newNode.southNode = baseNode;
                baseNode.northNode = newNode;
        }else if(newPos == Vector3.down)
        {
                newNode.northNode = baseNode;
                baseNode.southNode = newNode;
        }else if(newPos == Vector3.right)
        {
                newNode.westNode = baseNode;
                baseNode.eastNode = newNode;
        }else if(newPos == Vector3.left)
        {
                newNode.eastNode = baseNode;
                baseNode.westNode = newNode;
        }
    }
    private CardinalDirectionNode SelectNewNode(CardinalDirectionNode sourceNode, bool prevNodeAllowed)
    {
        List<CardinalDirectionNode> optionNodes = new List<CardinalDirectionNode>();
        if(sourceNode.northNode != null && (sourceNode.northNode != _prevNode || prevNodeAllowed)) optionNodes.Add(sourceNode.northNode);
        if(sourceNode.southNode != null && (sourceNode.southNode != _prevNode || prevNodeAllowed)) optionNodes.Add(sourceNode.southNode);
        if(sourceNode.westNode != null && (sourceNode.westNode != _prevNode || prevNodeAllowed)) optionNodes.Add(sourceNode.westNode);
        if(sourceNode.eastNode != null && (sourceNode.eastNode != _prevNode || prevNodeAllowed)) optionNodes.Add(sourceNode.eastNode);

        if(optionNodes.Count <= 0 ) return _prevNode;
        return optionNodes[Random.Range(0, optionNodes.Count)];
    }
 void Update()
 {
     if(_targetNode == null) return;
     if(transform.position != _targetNode.transform.position)
     {
         float distance = (_targetNode.transform.position - transform.position).magnitude;
         if(distance <= speed * Time.deltaTime)
         {
             transform.position = _targetNode.transform.position;
         }else{
             transform.Translate((_targetNode.transform.position - transform.position).normalized * speed * Time.deltaTime);
         }
     }else{
         CardinalDirectionNode newTarget = SelectNewNode(_targetNode, true);
         _prevNode = _targetNode;
         _targetNode = newTarget;
     }
 }
 public void StartWalking(CardinalDirectionNode node)
 {
     transform.position = node.transform.position;
     _prevNode = node;
     _targetNode = SelectNewNode(node, false);
 }