- Home /
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); }
}
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);
}
}
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);
}
}
This works BUT, if i pick up the paper, the hand is still shown, until i hit air
You need to add a second condition that checks if you picked up the paper. I update the solution.
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.
Answer by Landern · Nov 30, 2012 at 09:12 PM
if (hit.collider.gameObject.name != "paper") {
Drawhand = false;
}
Thanks for your fast answer, but i tried it before, and since the air isnt a collider, it wont work...
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. }
I tried the code, now its not showing the hand at all
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084752im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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