x


Best way to tell which object(s) are being looked at?

I have an app, let's say it's a 'Virtual Museum' type of thing. Lots of stuff to look around at.

I need to be able to tell what object(s) are being looked at, checking about twice a second.

Would it be best if

a) A script on the camera used Physics.Raycast logs which object(s) are 'hit'

b) A script on each 'object of interest' logs that the camera is looking at it (Collider.Raycast, right)?

c) Something else?

more ▼

asked Jan 19 '11 at 01:03 AM

DaveA gravatar image

DaveA
26.4k 151 171 256

Bo 'being looked at', do you just mean that the object is visible to the camera, or that it's more or less centered in the camera's view?

Jan 19 '11 at 01:09 AM Jesse Anders

More or less centered, clearly the 'focus of attention' at the moment. I realize this may mean casting several rays around the center (not just the exact center) of direction of view....

Jan 19 '11 at 01:14 AM DaveA
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

What I ended up doing was making a prefab which contains a box collider and script. On Update, check if ray cast from camera sees it, hits it. I figured this way, I'd be testing against a simple shape, it wouldn't affect my model prefab (sometimes need to reimport or swap them out), and I just put these around objects I care about. Seems to work pretty well.

more ▼

answered Jan 28 '11 at 04:29 PM

DaveA gravatar image

DaveA
26.4k 151 171 256

I think you probably choose the best and quickest method, especially if you have many detailed/complex meshes. On a project I recently did I tried making a offscreen Material ID shader/image, each GO was shaded in it's own unique color based on its unique Unity ID tag and rendered flatly offscreen, then the mouseposition would return the colour and match to the ObjID, had problems with z sorting though and never finished it.

Feb 09 '11 at 02:09 AM Jon-Martin
(comments are locked)
10|3000 characters needed characters left

You would probably want to use something thicker than a Raycast such as a capsule cast, if you are going to use some sort of a cast.

Another easy way would just be to use renderer.isVisible.

Renderer[] renderers = (Renderer[])(FindObjectsOfType(typeof(Renderer)));
//You probably want to cache this at the beginning.

foreach(Renderer r in renderers) {
      if(r.isVisible) {
            //Do something.
      }
}

Or, you could use the Dot product of the camera's forward Vector and the difference between the object's position and the camera's position. This would probably overly complicated, but Vector math is inexpensive for the most part, and if you can't use a the renderer method, then this might be a good idea. Personally, this is probably more complicated than you want to make it, but it's just another (albeit a bit strange) option.

Transform[] sceneObjects = (Transform[])(FindObjectsOfType(typeof(Transform)));

Vector3 thisObjPos = transform.position;
Vector3 forward = transform.forward;
//finding them now will save us from finding them when we iterate through.

foreach(Transform sceneObject : Transform in sceneObjects) {
      Vector3 offset = sceneObject.position - thisObjPos;
      offset = offset.normalized;

      If(Vector3.Dot(forward, offset) > someValue /*between -1, 1*/) {
           //Do stuff
      }
}
more ▼

answered Jan 19 '11 at 01:40 AM

Peter G gravatar image

Peter G
15k 16 44 136

(Original post edited for clarity) Capsule might work. isVisible wouldn't cut it for me, because there are many things visible. I need to know what they've 'gone up to and looked at' (and for how long). I figured raycasting has distance built into it. Although I also need to track where they stand, so was going to put proximity colliders around to track that, so I could combine proximity with isVisible. The Dot thing sounds interesting, will leave it for later if needed.

Jan 19 '11 at 01:47 AM DaveA
(comments are locked)
10|3000 characters needed characters left

Check if a screen rect contains the object's position using WorldToScreenPoint:

var percentageOfScreenHeight = .25;
private var centerRect : Rect;

function Start () {
    var ySize = Screen.height*percentageOfScreenHeight;
    centerRect = Rect(Screen.width/2 - ySize/2, Screen.height/2 - ySize/2, ySize, ySize);
}

function Update () {
    if (centerRect.Contains(Camera.main.WorldToScreenPoint(transform.position))) {
        Debug.Log ("Looked at");
    }
}

Since you only need it twice per second, you'd use InvokeRepeating instead of Update. You'd also want to make sure the objects are in front of the camera; the most optimal thing would probably be to make a List of transforms, and have the objects add/remove themselves to and from the list using OnBecameVisible and OnBecameInvisible, then cycle through that array (instead of having each object run that script). You can use Vector3.Distance to see if an object that's being looked at is close enough.

more ▼

answered Jan 19 '11 at 02:00 AM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

Wow, that's good advice. A problem I do have is that a lot the models came 'registered' to the building, meaning their origins are all at the same place. So the transform.position would be at the same place in the scene, for all objects. I'd get no hits unless I was looking at the origin, then I'd get a hit on all objects. Ack. I suppose I could pre-process the objects to find their world-coord centroid or bbox centers, and use that instead of transform.position.

Jan 19 '11 at 02:08 AM DaveA
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2983
x1525
x1089
x327
x11

asked: Jan 19 '11 at 01:03 AM

Seen: 1901 times

Last Updated: Jan 19 '11 at 01:43 AM