Hello. Quick preface, I read through the API on Update and FixedUpdate, but I didn’t fully understand why this changed my code… Basically, I wrote a code for a dialogue script which checks the distance via raycast and if its touching the Face collider, and initiates the chat if the player is close enough and all parameters are met. Let me be clear, this script works perfectly using FixedUpdate, but when using Update(), it doesn’t every time, almost as if there is lag and it only works some of the time…Can someone explain why?
if(Physics.Raycast(raycast.transform.position,(forward),out hit)){
print ("raycast hit");
Distance = hit.distance;
print (Distance + " " + hit.collider.gameObject.name);
if(Distance < 3 && hit.collider.gameObject.name == "Face" && Input.GetKeyDown ("e") && chat_on != true){
print ("E was pressed and chat is not true");
NPCS = GameObject.FindGameObjectsWithTag ("NPC");
foreach (GameObject obj in NPCS) {
objPos = obj.transform.position;
mag = (objPos - player.transform.position).sqrMagnitude;
if(nearestNPC == null || (nearestNPC.transform.position - player.transform.position).sqrMagnitude > mag){
print ("the mag calcs");
nearestNPC = obj;
}
}