- Home /
Finding when a Ray isn't hitting anything?
Hellooo, UnityAnswers.
Currently, I'm making a script wherein it casts a ray ahead of the player checking if there's anything tagged "Active" within a small range. If there is, it disables the characters' abilities attached to a certain button, and makes said button open the door instead. The issue I'm having now is that once an Active object is found, I can't reverse the abilities getting disabled if the player moves away from the object.
The way I figure, I could get it to work if I could tell when the Ray isn't hitting anything, and reversing the disabling there. How would I do that? Or is there a better way?
The script that checks for an Active object ahead of the player:
static var canSkill = true; static var canActivate = false;
function Update () {
var hit : RaycastHit;
if(Physics.Raycast (transform.position, transform.forward, hit, 2)){
if(hit.collider.tag == "Active") {
print ("Active");
canSkill = false;
canActivate = true;
}
}
}
Answer by Eric5h5 · Mar 26, 2011 at 08:35 PM
Use "else":
if (Physics.Raycast...) {
// hit
}
else {
// no hit
}
It also would probably be better if you did
if (Physics.Raycast (transform.position, transform.forward, hit, 2) && hit.collider.tag == "Active") {
Ah yeah. $$anonymous$$y bad; I often forget that 'else' even exists. Thanks, Eric!
Your answer
Follow this Question
Related Questions
How to register if my Raycast hits nothing(Solved) 2 Answers
How to hit two objects with only one shot (raycast) 0 Answers
Need help with Third person shooter 0 Answers
Ray from enemy to player is always a bit off (Image) 1 Answer
RaycastHit.distance 1 Answer