Reacting to eye hover in Reality kit and VisionOS

53 Views Asked by At

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?

1

There are 1 best solutions below

2
Jakob On BEST ANSWER

when you create your entity, just add a HoverEffectComponent to it. The entity also has to have both a CollisionComponent and InputTargetComponent.

let collisionShape = ShapeResource.generateBox(width: w, height: h, depth: d)
entity.components.set(HoverEffectComponent())
entity.components.set(InputTargetComponent())
entity.components.set(CollisionComponent(shapes:[collisionShape], isStatic: true))

you don't have to add a gesture for this to work. But it only works if these 3 components are present.

I found your question while trying to figure this out myself. The answer was in this video, which is worthwhile to watch in full: https://developer.apple.com/videos/play/wwdc2023/10080/