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 /
avatar image
0
Question by theFTWnetwork · Nov 30, 2012 at 09:00 PM · javascriptraycastcolliderair

If Raycast Hits Air

So, i have a script that will see if the raycast hits a object called "paper", if i come close to the objects, it draws a hond on the screen, works just fine, but when i look away (so it gous from hitting the paper, to hitting nothing(air)) the hand stays on the screen, i don't think i have to post any code, since i just want to know this in javascript:

if (hit.collider.gameObject.nothing)

Drawhand = false;

Edit: Code i have so far

var handdraw : boolean = false; var hand : Texture; var distanceToPaper : float = 2.5; function Start () {

}

function Update () { var ray = Camera.main.ScreenPointToRay( Vector3( Screen.width 0.5, Screen.height 0.5, 0.0 ) ); var hit : RaycastHit; if ( Physics.Raycast( ray, hit, distanceToPaper ) ) { if ( hit.collider.gameObject.name == "Paper" ) { handdraw = true;

          } 		if (hit.collider.gameObject.name !==

"Paper") {

         handdraw = false;
          }
      } }

function OnGUI(){ if (!hand){ Debug.LogError("Assign a Hand Bro!"); } if (handdraw == true) { GUI.DrawTexture(Rect(Screen.width/2-20, Screen.height/2-20,40,40), hand, ScaleMode.ScaleAndCrop, true, 1f); }

}

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

4 Replies

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

Answer by theFTWnetwork · Dec 01, 2012 at 05:18 PM

Okay, i got it to work by combining the answer from fafase, and my original script, this is the final result:

 var handdraw : boolean = false;  
 varhand : Texture;  
 var distanceToPaper : float = 2.5;  
 function Update () {    
   var ray =Camera.main.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height *0.5, 0.0 ) );     
   var hit : RaycastHit;     
   if ( Physics.Raycast(ray, hit, distanceToPaper ) ) { 
     if ( hit.collider.gameObject.name =="Paper" ) {
       handdraw=true;
     }
     else if( hit.collider.gameObject.name !="Paper" ){
       handdraw= false;
     }
   }
   else
     handdraw= false; 
   } 

  function OnGUI(){     
    if (!hand){
      Debug.LogError("Assign a Hand Bro!");
  }     
  if (handdraw) { 
    GUI.DrawTexture(Rect(Screen.width/2-20,Screen.height/2-20,40,40), hand,ScaleMode.ScaleAndCrop, true, 1f);    
  } 
 }
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
avatar image
1

Answer by fafase · Nov 30, 2012 at 09:49 PM

I think your issue is that you check if the raycast collides with the paper which is fine. But then you want to check collision with nothing. As you said, no possible.

The fact is either your raycast collides and if it is the paper then you draw. Or it does not collides and then you don't draw.

 var handdraw : boolean = false; 
 var hand : Texture; 
 var distanceToPaper : float = 2.5; 
 var picked:boolean =false;
 
 function Update () { 
    var ray = Camera.main.ScreenPointToRay(Vector3( Screen.width * 0.5, Screen.height * 0.5, 0.0 ) ); 
    var hit : RaycastHit; 
    if ( Physics.Raycast( ray, hit, distanceToPaper )&& !picked ) { 
       if ( hit.collider.gameObject.name == "Paper" ) {
          handdraw = true;
          if(Input.GetKeyDown(KeyCode.Space)){
             //Action to pick the paper and add to inventory
             picked = true; 
          }
       }
    }
    else 
       handdraw = false;

 }
 
 function OnGUI(){ 
    if (!hand){ Debug.LogError("Assign a Hand Bro!"); } 
    if (handdraw) { 
       GUI.DrawTexture(Rect(Screen.width/2-20, Screen.height/2-20,40,40), hand, ScaleMode.ScaleAndCrop, true, 1f); 
    }
 }
Comment
Add comment · Show 6 · 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 theFTWnetwork · Nov 30, 2012 at 11:00 PM 0
Share

This works BUT, if i pick up the paper, the hand is still shown, until i hit air

avatar image fafase · Dec 01, 2012 at 08:14 AM 0
Share

You need to add a second condition that checks if you picked up the paper. I update the solution.

avatar image theFTWnetwork · Dec 01, 2012 at 02:07 PM 0
Share

Still, its only going away when i hit air

avatar image fafase · Dec 01, 2012 at 04:03 PM 0
Share

Did you implement the picked variable and does it turn true when you pick the paper? If you did so, there is no reason for the first if to pass and skip the else.

avatar image theFTWnetwork · Dec 01, 2012 at 04:30 PM 0
Share

No, it doesnt turn to True when i pick up a paper

Show more comments
avatar image
0

Answer by Landern · Nov 30, 2012 at 09:12 PM

 if (hit.collider.gameObject.name != "paper") {
    Drawhand = false;
 }
Comment
Add comment · Show 3 · 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 theFTWnetwork · Nov 30, 2012 at 09:25 PM 0
Share

Thanks for your fast answer, but i tried it before, and since the air isnt a collider, it wont work...

avatar image Landern · Nov 30, 2012 at 09:28 PM 0
Share

i thought you where checking for an object called "paper" if you are na$$anonymous$$g the objects this should work. The above checks to ensure that if a collider is hit and the name of it is not paper then Drawhand = false;

i mean either you hit it if (hit.collider.gameObject.name == "paper"){ //... DO show hand. } else if (hit.collider.gameObject.name != "paper){ //... do not show hand. }

avatar image theFTWnetwork · Nov 30, 2012 at 09:30 PM 0
Share

I tried the code, now its not showing the hand at all

avatar image
0

Answer by Owen-Reynolds · Nov 30, 2012 at 09:50 PM

A basic raycast looks like this. Many times the else is left off, since if you don't get a hit, you do nothing:

 if(Physics.Raycast(....) {   } // got a hit, check what we hit, etc...
 else {  } // hit air

`hit.gameObject.name` is only filled in when you get a hit. If you hit air, it probably just has old data from the last time you did hit.

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

14 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

Related Questions

How to block dragging if collider hits 1 Answer

Trouble with RayCast, rigid body and the colliders tag 0 Answers

Raycasting fail 1 Answer

Bomberman like explosion 0 Answers

Accessing colliders, in if statement 2 Answers


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