Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by The_Uber_Rasta · Nov 05, 2014 at 04:32 AM · raycastbooleanraycasthitdetection

Raycast Hit not toggling variable if stops hitting

I have a script that needs to set a variable to true if Raycast is hitting a gameObject with tag and false if it is not , The issue is if I move the Generator object or the thing it is hitting and the Raycast is not hitting it anymore the variable is not changing to false


  private var Generator : GameObject;
     public var Power : float = 100;
     public var Current : float = 20;
     public var CanGivePower : boolean = false;
     public var GivingPower : boolean = false;
     public var HitObject;
     
     
     function Start () {
     
     }
     
     function FixedUpdate () {
     
         var fwd = transform.TransformDirection(Vector3.forward);
         var hit : RaycastHit;
         Debug.DrawRay(transform.position, fwd * 2, Color.red);
           if (Physics.Raycast (transform.position, fwd, hit, 2.00))
           {     
                if(hit.collider.gameObject.tag == ("Electric"))  
               {        
                     Debug.DrawRay(transform.position, fwd * 2, Color.green);
                     CanGivePower = true;
     
             
             }
              else if(!hit.collider.gameObject.tag == ("Electric"))  
             {
                 CanGivePower = false;
             }
           }
                     if(CanGivePower == true)
                     {
                     Debug.Log("Yes it is true");
                     }
                     else {
                     Debug.Log("No it is false");
                     }
     
     }








Comment
Add comment · Show 7
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 MD_Reptile · Nov 05, 2014 at 05:06 AM 0
Share

I've had similar problems. It's possible that the ray is ai$$anonymous$$g at an absolute position, rather than a position relative to its origin. I can't say for sure that's what is happening to you, but maybe try having an empty game object as a child of the generator, where you want the ray to aim, then use its position as your target for your raycast. Hope that helps!

avatar image incorrect · Nov 05, 2014 at 05:13 AM 0
Share

Probably that's because in your if-statement you check if something was hit. If nothing was hit your only line assigning false to the variable is not executed.

avatar image EvilTak · Nov 05, 2014 at 05:37 AM 0
Share

Your current code will toggle CanGivePower to false only if the raycast hits something, but not your desired object. If you want to toggle it to false when the raycast doesn't hit anything, you will have to add an else statement after the raycast if. Code:

 if(Physics.Raycast()){
 //Your current code
 }
 else
 CanGivePower = false;

avatar image EvilTak · Nov 05, 2014 at 06:02 AM 0
Share

But that code will only be run when the raycast is successful, isn't it? Look at the braces.

avatar image robertbu · Nov 05, 2014 at 06:05 AM 0
Share

@EvilTak - Yep.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by The_Uber_Rasta · Nov 05, 2014 at 11:42 AM

Wow thanks If I just realized that sooner but it makes sense if Raycast then it must be hitting something so I changed it now and added the else to the end of the raycast statement now it seems to be toggling just fine. Thanks

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

6 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Why is this giving me a null refrence exeption? -1 Answers

RayCast Boolean Help 1 Answer

Click to move script help 1 Answer

Raycast Coding Issues 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