Drag Gesture in Immersive Spaces with Reality Kit

I've been trying to get the drag gesture up and running so I can move my 3D model around in my immersive space, but for some reason I am not able to move it around. The model shows up in my visionOS 1.0 Simulator, but I can't seem to get it to move around. Would love some help with this and some resources too that would be helpful. Here's a snippet of my Reality View code

import SwiftUI import RealityKit import RealityKitContent

struct GearRealityView: View { static var modelEntity = Entity() var body: some View { RealityView { content in if let model = try? await Entity(named: "LandingGear", in: realityKitContentBundle) { GearRealityView.modelEntity = model content.add(model) } }.gesture( DragGesture() .targetedToEntity(GearRealityView.modelEntity) .onChanged({ value in GearRealityView.modelEntity.position = value.convert(value.location3D, from: .local, to: GearRealityView.modelEntity.parent!) }) ) } }

Answered by Vision Pro Engineer in 791131022

In order for an Entity to receive input events (such as a DragGesture) you need to add an InputTargetComponent. You also need to include a CollisionComponent to define the shape of the volume you can interact with:

if let model = try? await Entity(named: "LandingGear", in: realityKitContentBundle){
    GearRealityView.modelEntity = model

    // Choose a collision shape which fits the bounds of the object.
    // You can also create a CollisionComponent in Reality Composer Pro instead of at runtime.
    let collisionShape = ShapeResource.generateSphere(radius: 0.25)

    model.components.set([
        CollisionComponent(shapes: [collisionShape]),
        InputTargetComponent()
    ])
    content.add(model)
}

For an end-to-end example, check out the sample code Transforming RealityKit entities using gestures.

Accepted Answer

I realize the code snippet can be better formatted, so here's a better way to look at it

struct GearRealityView: View {

static var modelEntity = Entity()

var body: some View {

RealityView { content in

if let model = try? await Entity(named: "LandingGear", in: realityKitContentBundle) {

GearRealityView.modelEntity = model

content.add(model)

}

} .gesture(

DragGesture()

.targetedToEntity(GearRealityView.modelEntity)

.onChanged({ value in

GearRealityView.modelEntity.position = value.convert(value.location3D, from: .local, to: GearRealityView.modelEntity.parent!)

}) ) } }

In order for an Entity to receive input events (such as a DragGesture) you need to add an InputTargetComponent. You also need to include a CollisionComponent to define the shape of the volume you can interact with:

if let model = try? await Entity(named: "LandingGear", in: realityKitContentBundle){
    GearRealityView.modelEntity = model

    // Choose a collision shape which fits the bounds of the object.
    // You can also create a CollisionComponent in Reality Composer Pro instead of at runtime.
    let collisionShape = ShapeResource.generateSphere(radius: 0.25)

    model.components.set([
        CollisionComponent(shapes: [collisionShape]),
        InputTargetComponent()
    ])
    content.add(model)
}

For an end-to-end example, check out the sample code Transforming RealityKit entities using gestures.

Drag Gesture in Immersive Spaces with Reality Kit
 
 
Q