Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by DaveA · Jan 19, 2011 at 01:03 AM · cameraraycastobjectlookatlogging

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jesse Anders · Jan 19, 2011 at 01:09 AM 0
Share

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?

avatar image DaveA · Jan 19, 2011 at 01:14 AM 0
Share

$$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....

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jon-Martin · Feb 09, 2011 at 02:09 AM 0
Share

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.

avatar image
1

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
   }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Jan 19, 2011 at 01:47 AM 0
Share

(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.

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Jan 19, 2011 at 02:08 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges