RealityKit - How to change camera target in response of a touch event?

Hello, I’m porting my UIKit/SceneKit app to SwiftUI/RealityKit and I’m wondering how to change the camera target programmatically. I created a simple scene in Reality Composer Pro with two spheres. My goal is straightforward: when the user taps a sphere, the camera should look at it as the main target.

Following Apple’s videos, I implemented the .gesture modifier and it is printing the tapped sphere correctly, but updating my targetEntity state doesn’t change anything, so the camera won't update its target. Is there a way to access the scene content at that level? Or what else should I do?

Here’s my current code implementation:

Thanks!

Hello @Daniele Citi , thank you for your question!

Your code above looks correct, in that you are updating the targetEntity in response to the gesture, but I don't see any SwiftUI code that would observe the change to targetEntity. Try adding an update block to your view that observes changes to this variable, like this:

.update {
    content in
    content.cameraTarget = targetEntity
}

Let me know if that helps! Thank you!

RealityKit - How to change camera target in response of a touch event?
 
 
Q