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 Hyperactive · Nov 17, 2015 at 10:50 AM · raycastraycasthit

Detect when object is no longer hit by Raycast

Hi All,

At first this seem like a simple idea, but I can't seem to figure out how to do it.

I have a raycast, and when it hits an object with tag "key", it sets a boolean in script on that object to TRUE. Easy. What I'd like is for when the the raycast stops hitting that same object, the boolean reverts to FALSE.

Here's what I got so far, amongst many other bizarre attempts.

  private bool Key;
 
  public Camera camera;
  
  void Start() {
         camera = GetComponent<Camera>();     
     }
     
     void Update() {
         Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
         RaycastHit hit;   
  {
  if ((Physics.Raycast(ray, out hit, 100)) && (hit.collider.tag == "Key"))
  {
         Key = hit.collider.gameObject.GetComponent<Activated>().active;
         Key = true;
  }
  else if (Key != null)
  {
      Key = false;
  }
  
 }
 }
 
 }

Comment
Add comment · Show 4
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 MayankOctro · Nov 17, 2015 at 10:59 AM 0
Share

put $$anonymous$$ey = false; in in your Update first line. problem solved.

avatar image Hyperactive MayankOctro · Nov 17, 2015 at 11:08 AM 0
Share

Thanks for the comment, but that will just lock that Boolean to FALSE if I have it at the start of the Update function.

avatar image MayankOctro Hyperactive · Nov 17, 2015 at 11:16 AM 0
Share

But if your condition is correct it would make it true also.

Show more comments

2 Replies

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

Answer by HenryStrattonFW · Nov 17, 2015 at 06:04 PM

Try swapping your update to this.

 void Update() 
     {
         Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
         RaycastHit hit;   
         {
             if (Physics.Raycast(ray, out hit, 100))
             {
                 if(hit.collider.tag == "Key")
                 {
                     Key = hit.collider.gameObject.GetComponent<Activated>().active;
                     Key = true;
                 }
                 else
                 {
                     // Hitting something else.
                     Key = false;
                 }
             }
             else if (Key == true)
             {
                 // not anymore.
                 Key = false;
             }
         }
     }

This will ensure that the Key bool will get set to false in either of the two cases:

  1. We didn't hit anything, therefore we didn't hit the key.

  2. We hit something that isn't the key, therefore we aren't hitting the key.

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 Hyperactive · Nov 20, 2015 at 04:51 PM 0
Share

This works perfectly ! Thanks a lot. In the meantime, I did find another way around it, buy resetting the $$anonymous$$ey to null in the if else.

What I'm trying to add to this now though is to be able to keep the key on trueif I need to. So, for example, if you press the space bar while the object is getting hit by the raycast and $$anonymous$$ey is set to true, then the key stays on true even after you stop looking at it. I'm trying this, which always goes back to false for some reason:

 private bool keyStop = false;
 
 void Update() 
      {
          Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
          RaycastHit hit;   
          {
              if (Physics.Raycast(ray, out hit, 100))
              {
                  if(hit.collider.tag == "$$anonymous$$ey")
                  {
                      $$anonymous$$ey = hit.collider.gameObject.GetComponent<Activated>().active;
                      $$anonymous$$ey = true;
 
                   if (Input.Get$$anonymous$$eyDown("space"))
                         {
                         $$anonymous$$eyStop = true;                      
                         } 
 
                  }
                  else if (keyStop == false)
                  {
                      // Hitting something else.
                      $$anonymous$$ey = false;
                  }
              }
              else if (($$anonymous$$ey == true) && (keyStop == false))
              {
                  // not anymore.
                  $$anonymous$$ey = false;
              }
          }
 









avatar image
0

Answer by starikcetin · Nov 17, 2015 at 06:39 PM

 private bool hitFlag = false;

 void Update()
 {        
     if(Physics.Raycast(...))
     {
         hitFlag = true 
     }
     else if (hitFlag)
     {
         hitFlag = false;
         // there you go. here we have object hitted on previous frame but not on this frame.
     }
 }


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

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

35 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

Related Questions

Raycast script help? 1 Answer

Raycast Coding Issues 1 Answer

Mesh polygon of a hit surface on a gameobject 1 Answer

How to place an object in the direction of a raycast 1 Answer

ray cast returning the wrong object, going through multiple objects 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