Search code examples
focusrealitykitvisionos

Reacting to eye hover in Reality kit and VisionOS


I know that all swiftUI views and buttons can react to where the user is looking in visionOS, but I'd like to do the same thing with entities in RealityKit, so users know what they are looking at. I can see that in some games downloaded from the store, 3d buttons seem to be reacting and glowing, I'm just not sure how they are doing it. Are these attached swiftUI views or is there a way to register entities for focus?


Solution

  • Adding to @Jakobs answer. When you work with Scenes and have complicated models, instead of creating a CollisionShape you probably want the whole Entity to be hover/clickable. To achieve this use the generateCollisionShapes function with recursive: true. This way the whole CollisionShape will become the whole Entity.

    let entity = rootEntity.findEntity(named: "SomeName")!
    entity.components.set(HoverEffectComponent())
    entity.components.set(InputTargetComponent())
    entity.generateCollisionShapes(recursive: true)