Hello. Basically, i evaluate distance from one point to others, then i move this point and evaluate this distance anew, but it return the same result (as if it was not moved, but it certainly was). Why could this be? Maybe, I just not understand something crucial about Update()?
=======================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class plControl : MonoBehaviour
{
RaycastHit hit;
Vector3 clHex;
[SerializeField] float plSpeed = 1f;
[SerializeField] GameObject pFinder;
public GameObject[] hexes = new GameObject[20];
public GameObject[] nearHex = new GameObject[7];
public Vector3[] path = new Vector3[5];
private Vector3 pfCor; //pathfinder Y-height correction
int pfStepNo;
void Awake()
{
clHex = pFinder.transform.position;
pfCor = pFinder.transform.position + Vector3.down;
pFinder.GetComponent<Light>().color = Color.blue;
}
void Start()
{
nearHexSearch();
pfStepNo = 1;
}
void Update()
{
Ray mRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0)) //click detect
{
if (Physics.Raycast(mRay, out hit))
{
clHex = hit.collider.transform.localPosition;
StartCoroutine("pfRoutine");
// pfStepNo++;
}
}
}
void nearHexSearch() // +correction to hex pos (x,0,z)!!!
{
int j = 1;
for (int i = 1; i <= hexes.Length -1; i++)
{
if (Vector3.Distance(hexes*.transform.position, pfCor) < 1.2f)*
{
if (hexes*.transform.position != pfCor)*
{
nearHex[j] = hexes*;*
j++;
}
}
}
}
void pathFind()
{
int s = 1;
path[pfStepNo] = nearHex[1].transform.position;
float nearest = Vector3.Distance(nearHex[1].transform.position, clHex);
for (int i = 1; i <= nearHex.Length - 1; i++)
{
if (Vector3.Distance(nearHex*.transform.position, clHex) < nearest)*
{
path[pfStepNo] = nearHex*.transform.position;*
nearest = Vector3.Distance(nearHex*.transform.position, clHex);*
s = i; //nearest hex number in nearHex[]
}
}
//print(nearest);
pFinder.transform.position = nearHex~~.transform.position + Vector3.up;~~
}
IEnumerator pfRoutine()
{
pathFind();
System.Array.Clear(nearHex, 0, nearHex.Length);//ok
yield return new WaitForSeconds(0.5f);
nearHexSearch();
}
}