Drag Gesture
A simple example of using DragGesture to move entities.
In this example we create a drag gesture that can target entities to allow us to move them in our scene.
struct Example007: View {
var body: some View {
RealityView { content in
// Load the scene from the Reality Composer Pro project
if let scene = try? await Entity(named: "GestureLabs") {
content.add(scene)
}
}
.gesture(dragGesture)
.realityViewLayoutBehavior(.fixedSize)
.volumeBaseplateVisibility(.hidden)
}
var dragGesture: some Gesture {
DragGesture()
.targetedToAnyEntity()
.onChanged { value in
value.entity.position = value.convert(value.location3D, from: .local, to: value.entity.parent!)
}
}
}Video Example
This is the most basic example of moving an entity. You may notice the entity “snap” from its current position when starting this gesture. This is because the location3D value from the gesture isn’t based on the position of the entity, but the position of the gesture on the entity.
Learn how to fix that by adding the initial position to the movement.
Support our work so we can continue to bring you new examples and articles.

Follow Step Into Vision