Do the entities that you want to drag have colliders and InputTargetComponents? If so you can use the entity value on the tap and drag gesture directly. TargetedToAnyEntity requires an input target component to trigger.
The below lets you tap on a box to change the material and drag a box to a different location.
I believe both Tap and Drag gestures have a hitTest method that you can use as well.
import SwiftUI
import RealityKit
struct ContentView: View {
@State var root: Entity = Entity()
@GestureState private var isDragging: Bool = false
var body: some View {
ZStack(alignment: .bottom) {
RealityView { content in
content.add(root)
for idx in 0...3 {
let ball = ModelEntity(mesh: .generateBox(size: 0.1), materials: [SimpleMaterial(color: .blue, isMetallic: false)])
ball.generateCollisionShapes(recursive: true)
ball.components.set(InputTargetComponent())
ball.position.y = 0.3 * Float(idx)
root.addChild(ball)
}
}
Button("Reset") {
for entity in root.children {
guard entity.components.has(InputTargetComponent.self)
else { continue }
entity
.components[ModelComponent.self]?.materials = [
SimpleMaterial(color: .blue, isMetallic: false)
]
}
}
}
.gesture(TapGesture().targetedToAnyEntity().onEnded{ tap in
tap.entity
.components[ModelComponent.self]?.materials = [
SimpleMaterial(color: .green, isMetallic: false)
]
})
.gesture(DragGesture(coordinateSpace: .global)
.targetedToAnyEntity().updating($isDragging) { value, state, _ in
if let location3d = value.unproject(value.location, from: .global, to: .scene) {
value.entity.setPosition(location3d, relativeTo: root)
}
})
}
}
#Preview {
ContentView()
}