Okay, problem 1 is that I can’t tell where your “ray” in your raycast is being defined. A ray is an object, and it needs to be created- it’s not a keyword or something. Also, copy the entire script into a post here and put it in [code ][/code ] tags so I can look it over. Make sure “using UnityEngine;” is defined at the top too. Write out the whole error too, or rather the part about what namespace it’s looking for or where it’s pointing at as “wrong”- not enough information.
General Overview:
Basically what you’re doing is your taking your player’s current position (could be the camera if it’s first-person) and you’re going to shoot an invisible laser right out in front of you and see what you hit. This requires several different parts, the biggest of which is that the object you’re trying to hit has a Collider attached to it. If it doesn’t have a Collider, the raycast can’t see it, so it can’t hit it. You can go into the inspector and add a component to the door object called a “Box Collider” and set it visually to be more or less overlapping the frame of the door, make sure it’s turned on, and that’s it for that.
Back in your script, you need to define a new “ray”. A ray is like a laser- it’s made up of an origin point and a direction. Luckily for us, there’s a shortcut to making a ray shoot out straight from the middle of the viewport, and it’s called Camera.ViewportPointToRay. All you have to do is feed it the viewport coordinates as a parameter (viewport coordinates are “percentage of the screen”, from 0 to 1, starting from bottom left) for the center of the screen. That’s “new Vector2(0.5f, 0.5f)” for exactly half each way. That’ll give us a ray positioned at the middle of the viewport (but in world coordinates) and pointing in the same direction as the camera. Cake!
Ray ray = Camera.ViewportPointToRay(new Vector2(0.5f, 0.5f));
RaycastHit hit;
Now the raycast is the act of shooting that ray and trying to hit something with it. That takes several parts, the first of which is the ray itself (you can also pass in the origin point and direction manually here, rather than making the ray first, and it’ll make it internally- same difference). The next parameter is your “RaycastHit” that you’ll define first. If the raycast hits anything, the information about what’s been hit will be stored in that variable. You can also specify a maximum distance to shoot the ray, and a filter for which layers the ray can interact with. The max distance is automatically set to Mathf.Infinity (go forever) if you don’t want to worry about it.
Physics.Raycast(ray, hit)
{
}
Finally, when and if a hit occurs, it’ll go into the “if” conditional you’ve set and you can check the tag of the object. What you have there looks far too complicated- you can check the tag with a simple:
if(Physics.Raycast(ray, hit))
{
if(hit.collider.tag == "Door")
{
}
}
The use of a conditional operator here with:
useNormalCursorTexture ? cursorTextureNormal : cursorTextureNo
should be destroyed with all of the vehemence you can muster. This kind of thing makes for confusing code that’s hard to read, and nothing more. If you need a conditional statement, just write the whole statement out, don’t bother with this kind of shortcut. Here’s one way to do this:
if(hit.collider.tag == "Door")
{
SetCursorTexture(cursorTextureNo);
}
else
{
SetCursorTexture(cursorTextureNormal);
}
And you need to place the “normal” setter as an “else” case to your raycast as well, so it also changes back if the raycast hits nothing at all, and not just if it hits something but that something isn’t a “Door”.