Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
gskohner's avatar
gskohner
Honored Guest
12 years ago

crosshair over objects

Hi all,

How can i detect if the crosshair is over a specific object from a scene?

I want to implement something like "look at an object and interact with it".

Thank you.

4 Replies

Replies have been turned off for this discussion
  • drash's avatar
    drash
    Heroic Explorer
    There's probably a lot of ways to solve this, but here's what I would do:
    • Add a collider to the object you want to look at/interact with (if it doesn't already have one).

    • Create a script where, in every Update(), you do a Physics.Raycast() from one of the Rift camera's transform.position (or even better, the midpoint between two two cameras) out along the direction of that camera's transform.forward, then check the resulting "hit info" to see if you hit that collider (or a different collider or nothing at all) and act accordingly.


    Hope that helps!
  • "drash" wrote:
    There's probably a lot of ways to solve this, but here's what I would do:
    • Add a collider to the object you want to look at/interact with (if it doesn't already have one).

    • Create a script where, in every Update(), you do a Physics.Raycast() from one of the Rift camera's transform.position (or even better, the midpoint between two two cameras) out along the direction of that camera's transform.forward, then check the resulting "hit info" to see if you hit that collider (or a different collider or nothing at all) and act accordingly.


    Hope that helps!

    Yep that's one way of doing it. It may be advisable to do a spherecast, to make it a less strict test.
  • drash's avatar
    drash
    Heroic Explorer
    "boone188" wrote:
    Yep that's one way of doing it. It may be advisable to do a spherecast, to make it a less strict test.


    TIL Spherecast! "Think of the sphere cast like a thick raycast." /unity docs.

    Awesome, thanks for the tip. :)