• 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 /
  • Help Room /
avatar image
0
Question by ngerbens · Jan 22, 2018 at 10:43 PM · uiraycastraycastingobjectsraycasthit

Switching from object to object with Raycast Problem

Hello everyone reading this.

I hope someone knows how to crack this!

I use a Raycast that makes my enemies(asteroids) HealthUI pop up.
When my players Raycast deselects the asteroid, my UI disables which is perfect.
BUT: When I go select an asteroid after another asteroid (with no space in between) the UI of the previous asteroid stays on screen. I tried a lot of things. But obviously just not the right thing.

Here's my script:

 public Transform brickPlayer;
     public LineRenderer lineRenderer;
     public LayerMask mask;
     private AsteroidUI1 AsteroidUI;                
     public Transform NewHitObject;
 
 
     void FixedUpdate()
     {
         Ray ray = new Ray (transform.position, transform.forward);
         RaycastHit hitInfo;
 
         if (Physics.Raycast (ray, out hitInfo, mask)) 
         {
             Debug.DrawLine (ray.origin, hitInfo.point, Color.red);
             NewHitObject = hitInfo.collider.transform;
 
             if (hitInfo.collider.tag == "Asteroid") 
             { 
                 lineRenderer.SetPosition (0, brickPlayer.position);
                 lineRenderer.SetPosition (1, hitInfo.point);
                 lineRenderer.enabled = true;
 
                 ShowAsteroidUI ();
             } 
         } 
         else
         {
             if (NewHitObject == true) 
             {
                 StopShowAsteroidUI ();
             }
 
             Debug.DrawLine (ray.origin, ray.origin + ray.direction * 100, Color.green);
             lineRenderer.enabled = false;
         }
     }
 
 
     public void ShowAsteroidUI()
     {
         NewHitObject.GetComponent<AsteroidUI1> ().ShowUI ();
     }
 
     public void StopShowAsteroidUI()
     {
         NewHitObject.GetComponent<AsteroidUI1> ().StopShowUI ();
     }



Comment
Add comment
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

1 Reply

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

Answer by Alanisaac · Jan 24, 2018 at 12:24 AM

Hopefully this is what you're after. I essentially added a local variable to store the "old" hit object before things get going, then used that variable to first stop the old hit object's UI in the case where you find a new hit object.

      public Transform brickPlayer;
      public LineRenderer lineRenderer;
      public LayerMask mask;
      private AsteroidUI1 AsteroidUI;                
      public Transform NewHitObject;
  
  
      void FixedUpdate()
      {
          Ray ray = new Ray (transform.position, transform.forward);
          RaycastHit hitInfo;
 
          // store the hit object in a local variable
          var oldHitObject = NewHitObject;
  
          if (Physics.Raycast (ray, out hitInfo, mask)) 
          {
              Debug.DrawLine (ray.origin, hitInfo.point, Color.red);
              NewHitObject = hitInfo.collider.transform;
  
              if (hitInfo.collider.tag == "Asteroid") 
              { 
                  lineRenderer.SetPosition(0, brickPlayer.position);
                  lineRenderer.SetPosition(1, hitInfo.point);
                  lineRenderer.enabled = true;
  
                  // hide the old hit object UI before showing the new one
                  StopShowAsteroidUI(oldHitObject);
                  ShowAsteroidUI(NewHitObject);
              } 
          } 
          else
          {
              if (oldHitObject == true) 
              {
                  // clear the hit object just so this code doesn't execute over and over
                  NewHitObject = null;
                  StopShowAsteroidUI(oldHitObject);
              }
  
              Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100, Color.green);
              lineRenderer.enabled = false;
          }
      }
  
  
      public static void ShowAsteroidUI(Transform t)
      {
          t.GetComponent<AsteroidUI1>().ShowUI();
      }
  
      public static void StopShowAsteroidUI(Transform t)
      {
          t.GetComponent<AsteroidUI1>().StopShowUI();
      }
 
Comment
Add comment · Show 1 · 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 ngerbens · Jan 24, 2018 at 08:56 AM 0
Share

Hi @Alanisaac , Thank you very much for this solution.
As my 'NewHitObject' transform implies, I already tried it with an extra 'oldHitObject' private Transform, but with no succes. (I cleared it out to post a 'clean' script) $$anonymous$$aybe due to where I positioned my lines but probably mainly because I didn't pass the values to the public voids. Long story short: I learned a lot, thanks again A LOT for giving peace to mind. ;-) Cheers!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

203 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 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

Help with Raycast Script 0 Answers

Raycast bug is making me go nnuts,Raycast bug is making me go nuts 0 Answers

Adding MaxDistance to RayCast Stops Raycast From working 0 Answers

NullReferenceException while raycasting? 0 Answers

Why isn't my ray working? 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