Easy raycast question

Hi – I’m trying to create a script that will tell me the distance of the camera to a specific gameobject. I’m really close - the script below does everything I want, except that instead of the ray being cast from the viewport, I want it to be cast uniformly in all directions (so the object will be detected even if the camera is looked the opposite direction). I know this is easy but I’ve tried several variations based on the docs and and just stumbling around it - please, any help is appreciated.

var hitTarget : Transform;
function Update () {
// the line below is the one I think I need to change.
var ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0));
var hit : RaycastHit;
var distance : float;
if (Physics.Raycast (ray, hit)) {

if(hit.transform.name == hitTarget.name){
if(hit.distance < 3){
print("hit the object at distance of " + hit.distance);

}
}
}
}

You could always just do a distance check between the camera’s position and the object’s position instead of shooting a ray.

Either that or shoot rays directly at each object you want to detect using the camera position as the origin.

You can’t really shoot a million different rays out in all directions every frame checking for a collision.

Hope this helps

Thanks for the response Chris – yeah, I thought of the distance check – you’re right, its basically what I want, but the distance gets determined from the center of the object – mine’s pretty wide, so if the camera is at the end (close to the object, but far away from its center) it doesn’t work. Any way to do a distance check so that any part of the object counts?

Appreciate your time.

In that case, I think your best bet would be to just cast a ray towards all the objects you want to detect in your scene, using the camera as your origin.

So for each object something like:

Physics.Raycast(camera.transform.position, directionToObject, …)

Thanks Chris – I’ll start working on something like that.

Assuming you’re just checking if anything is within X distance of your camera (on all sides). You could add a sphere collider to your camera of the appropriate size (or add a child object and remove its renderer), set isTrigger and check for that firing?

That’s a really interesting idea and I think that might be the solution - I’ll try it tonight! I tried an approach using a collider but the collider kept me from moving the camera close to places I wanted to get to. BUT I didn’t understand what the isTrigger was all about – seems like that might fix it. Thanks Flimgoblin!

So I tried the collider and I’m almost there. The collider does what I want, but the problem now is that the collider obstructs another object that has a mousedown script on it so it doesn’t work now(colliders prevent mousedown events I guess?)

To clarify: I have a picture on a wall. I’m using the collider to prevent the user from being too close to the wall when they click the picture (it screws things up if they’re too close). But the wall collider covers the picture so it can’t be clicked. Is there a way to pass clicks through colliders? Thanks again – this is kind of boggling my mind.

You can put the collider object on the Ignore Raycast layer to make it… well, ignore raycasts.

Thanks for the response Andeeee - let me back up a minute because my problem’s evolved. Here’s a clearer explanation of what I’m trying to do:

  1. I have a wide wall.
  2. On the wall is a painting. If the user clicks it, it floats up to the camera (which is now frozen), they can examine it, then click a GUI button to put it back on the wall and unfreeze the camera. All this works pretty well.
  3. But, if the camera is too close to the picture or the wall its on and the user clicks the painting, its awkward because the painting flys into a weird position … so I want to disable the mousedown on the painting if the camera is too close to the painting or the wall.
  4. Got it working fine for the painting by using Vector3.Distance, but that doesn’t work for the wall - I believe because Vector3.Distance uses the postion of the wall, but the problem occurs if the camera is anywhere along the wall (not just its center.) If they’re too close to the wall, and looking at it, the painting flys up and gets trapped (or something else undesirable happens).
  5. So I tried using rayacast which is what my original not was about. Now I’ve been messing with colliders. Putting an extended collider on the wall seems to be exactly what I need - it keeps the camera at a good distance. But now the click on the painting doesn’t work any more.
  6. I tried putting the wall on a different layer, and the click gets noticed, but now the painting flys all the way up into the camera, ignoring the click on it that tells it to stop.

Anyway, I know this is a big jumble – thanks to everyone who gave input. Any additional thoughts off the top of your head would really helpful …

Noob