- Home /
Raycast Hit Not Working (JS)
Hello,
I have a script that is supposed to be able to open an airlock door. Ideally, when you mouse over the door, a gui that was previously inactive shows and says "Open Airlock". The problem is that the GUI Text "Open Airlock" does not become active when the raycast is pointed at the airlock. I have fiddled around with the script and came to the conclusion that the faulty part wasn't with the raycast but was where it was saying if (hit.transform == airlockDoor). It doesn't come up as a script error in Unity, it just doesn't work. Please help! Here is the script:
#pragma strict
var airlockDoor : Transform;
var airlockDoorInside : Transform;
var openAirlockText : GameObject;
function Start () {
openAirlockText.SetActiveRecursively(false);
}
function Update () {
var hit : RaycastHit;
var fwd = transform.TransformDirection (Vector3.forward);
if (Input.GetKey (KeyCode.F) && Physics.Raycast (transform.position, fwd, 3) ) {
if (hit.transform == airlockDoor) {
airlockDoor.SendMessage("OpenAirlock", SendMessageOptions.DontRequireReceiver);
}
if (hit.transform == airlockDoorInside)
airlockDoorInside.SendMessage("CloseAirlock", SendMessageOptions.DontRequireReceiver);
}
if (hit.transform == airlockDoor)
openAirlockText.SetActiveRecursively(true);
if ((hit.transform == airlockDoor) == false)
openAirlockText.SetActiveRecursively(false);
}
Thanks. Any help or solution to this problem, even in C#, is very much appreciated.
Answer by thevraptor · Jul 24, 2014 at 08:21 AM
Problem solved, everyone! Turns out that for some reason, setting game objects active under a raycast doesn't work, so I relayed it through a variable using the hit.transform and it worked like a charm! Thanks, Rutter!
#pragma strict
var airlockDoor : Transform;
var airlockDoorInside : Transform;
var openAirlockText : GameObject;
var mlem : boolean = false;
function Start () {
openAirlockText.SetActiveRecursively(false);
}
function Update () {
var hit : RaycastHit;
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast(transform.position, fwd, hit, 1) ) {
mlem = true;
print ("mlem");
if (hit.transform == airlockDoor) {
openAirlockText.SetActiveRecursively(true);
}
if (hit.transform == airlockDoorInside)
airlockDoorInside.SendMessage("CloseAirlock", SendMessageOptions.DontRequireReceiver);
}
if (hit == true)
print ("mlem");
if (Physics.Raycast(transform.position, fwd, hit, 1) == false)
mlem = false;
if ((hit.transform == airlockDoor) == false)
openAirlockText.SetActiveRecursively(false);
if (mlem == true && hit.transform == airlockDoor)
openAirlockText.SetActiveRecursively(true);
}
Answer by rutter · Jul 21, 2014 at 09:16 PM
There are many different "overloaded" versions of the Raycast function. You can view all of them at the scripting manual, or in MonoDevelop once you start typing out the function call.
You need to call one which populates the RaycastHit data. Perhaps something like this:
Physics.Raycast(transform.position, fwd, hit, 3)
As an aside, the raycast hit will only be populated if the raycast actually hits something (ie: the call returns "true"). It's potentially unsafe to access the hit data if you don't know for sure that it's valid.
Finally, the script may act a bit unintuitively if you've forgotten to set airlockDoor
or airlockDoorInside
in the Inspector.
Thanks, but I just tried that but with no luck. I do have the transforms set to the doors in the Inspector, but the GUIText still will not pop up. Is this possibly a glitch in the game engine? Or am I just stupid?
Hopefully none of the above! The physics engine is pretty complicated, with a lot of moving parts. $$anonymous$$y main suggestion is to test all of your assumptions. Do these objects have colliders attached? Are they on a layer that ignores raycasts? Are you casting to/from the exact positions you expect? Are you hitting an object other than the one you expect? Are these raycasts even being fired in the first place?
The Debug class can be very helpful. Especially the DrawLine, DrawRay, and Break functions, if you haven't tried them yet.
Ok, so I've tampered around with the script a little bit more, and here is the script in which I figured out what the problems were:
var airlockDoor : GameObject;
var airlockDoorInside : Transform;
var openAirlockText : GameObject;
function Start () {
openAirlockText.SetActiveRecursively(false);
}
function Update () {
var hit : RaycastHit;
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast(transform.position, fwd, hit, 3) ) {
openAirlockText.SetActiveRecursively(true);
print ("mlem");
if (hit.transform == airlockDoor) {
openAirlockText.SetActiveRecursively(true);
}
if (hit.transform == airlockDoorInside)
airlockDoorInside.Send$$anonymous$$essage("CloseAirlock", Send$$anonymous$$essageOptions.DontRequireReceiver);
}
if (hit == true)
print ("mlem");
if ((hit.transform == airlockDoor) == false)
openAirlockText.SetActiveRecursively(false);
}
Now, there's two problems with this script. When it's casting the ray and returns a hit under the raycasting if, it prints mlem as it's supposed to, but doesn't make the gui text ( the open airlock text var) active. The other problem is that when I remove said print ("mlem") and leave the one at the bottom as is, it doesn't print mlem as it is supposed to. So, if the hit var doesn't work, then hit.transform can't work. The problem with the gui text not beco$$anonymous$$g active is extremely confusing to me, as it should be working. I have other gui text in the scene, and they work fine in that context. I'm also not sure why the hit var is not working.
Your answer
Follow this Question
Related Questions
RaycastHit: What is the difference between hit.transform.tag and hit.collider.tag? 1 Answer
Difference between hit.collider.gameObject vs hit.transform.gameObject 2 Answers
is hit.transform always the same as hit.transform.gameObject.transform 1 Answer
Finding a RaycastHit tag (Null Reference Exception) 2 Answers
RaycastHit info works using print() but not GUIText? 1 Answer