Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mega_Jimmy · Jul 29, 2018 at 03:50 PM · raycasthit detection

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mega_Jimmy · Jul 29, 2018 at 05:03 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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! 

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mega_Jimmy · Jul 29, 2018 at 07:43 PM 0
Share

That seems to have done it! Rookie mistake, I guess? Thanks!

avatar image Carterryan1990 Mega_Jimmy · Jul 29, 2018 at 09:57 PM 0
Share

Definitely a rookie mistake, I use to do it all the time myself, no problem good luck!

avatar image araz01 · Mar 20, 2020 at 06:54 PM 0
Share

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

avatar image nir11 · Nov 04, 2020 at 01:15 AM 0
Share

Thank you just helped me immensely!!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges