- Home /
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?
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?
$$anonymous$$ore 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....
Answer by DaveA · Jan 28, 2011 at 04:29 PM
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.
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 $$anonymous$$aterial 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.
Answer by Peter G · Jan 19, 2011 at 01:40 AM
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
}
}
(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.
Answer by Eric5h5 · Jan 19, 2011 at 02:00 AM
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.
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 ins$$anonymous$$d of transform.position.
Your answer
Follow this Question
Related Questions
Make the terrain ignore Raycast if in between Camera and Player? 1 Answer
Start raycast from object 1 Answer
Camera rotate to look at GameObject from Raycast 3 Answers
Is object at least partly visible? 1 Answer
How do I keep functionality of LookAt script whilst using it in a 3dGUI camera layer? 0 Answers