- Home /
RayCast stops working after detecting a hit
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!
If I were to do it as a Bool ins$$anonymous$$d;
bool IsGrounded()
{
return Physics.Raycast(transform.position, transform.right, 2f);
}
Is it still possible to detect whether the RayCast is hitting an NPC?
Answer by Carterryan1990 · Jul 29, 2018 at 05:07 PM
here try this
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);
}
}//you forgot this bracket, the else statesment needs to involve the raycast not the tag
else
{
Debug.Log("not detecting anything...");
talkYes = false;
pin.GetComponent<masterScript>().Invoke("iconDelete", 0f);
}
}
}
//if this works for you please mark my answer as the correct answer, thanks!
That seems to have done it! Rookie mistake, I guess? Thanks!
Definitely a rookie mistake, I use to do it all the time myself, no problem good luck!
what was the issue lol, i gotta sit her efor 10 $$anonymous$$uts going though each line i need to sleep lets start there lol
Your answer
Follow this Question
Related Questions
Raycast with a mouse position! 1 Answer
Raycast hits wrong object 1 Answer
How do I calculate hit detection between 2 lines? 0 Answers
Simulating Bullet Gravity using Raycasting 1 Answer
Raycast, Calculate Random Direction 1 Answer