I’m trying to implement an icon to indicate to the player that an NPC character can be spoken to when they’re within a set distance.
The way I’ve set it is that during FixedUpdate is uses a RayCast to look for a character with an NPC Tag. It then invokes a canvas script to place the icon over the character’s head.
The problem I’m having is that I obviously want the icon to vanish after the player moves away or when the actual textbook loads onscreen.
The RayCast detects the hit and sets up the icon but then seems to stop casting afterwards. Is this normal? Here’s my code below;
private void FixedUpdate()
{
Ray ray = new Ray(transform.position, transform.right);
if (Physics.Raycast(ray,out hit, 1))
{
if (hit.collider.tag.Equals("NPC"))
{
talkYes = true;
hitheight = hit.collider.GetComponent<DialogueTrigger>().height;
pin.GetComponent<masterScript>().NPCTF = new Vector3(hit.transform.position.x, hit.transform.position.y + hitheight, hit.transform.position.z);
pin.GetComponent<masterScript>().Invoke("iconSpawn", 0f);
}
else
{
Debug.Log("not detecting anything...");
talkYes = false;
pin.GetComponent<masterScript>().Invoke("iconDelete", 0f);
}
}
}
TalkYes is how the game knows the character can be spoken with (and has an actual Script attached) whereas the pin mentioned is the canvas object I have set up which places the icon in question.
However once it detects the hit, no matter what I do it seems to stop. I’m just looking for a way to get that “else” part to function. I’ve run a few tests but nothing I do seems to work.
Thank you for reading!