Rotate Gesture 3D

Using the RotateGesture3D to rotate entities around an axis.

Let’s start with a simple example of rotating an entity around the Y axis.

struct Example009: View {
    var body: some View {
        RealityView { content in
            // Load the scene from the Reality Kit bundle
            if let scene = try? await Entity(named: "GestureLabs", in: realityKitContentBundle) {
                content.add(scene)

                // Lower the entire scene to the bottom of the volume
                scene.position.y = -0.4
            }
        }
        .gesture(rotateGesture)
    }

    var rotateGesture: some Gesture {
        RotateGesture3D(constrainedToAxis: .y)
            .targetedToAnyEntity()
            .onChanged { value in

                let rotation = value.rotation
                let rotationTransform = Transform(AffineTransform3D(rotation: rotation))
                value.entity.transform.rotation = rotationTransform.rotation

            }
    }
}

Issue: This doesn’t take into account the initial orientation of the entity. If the entity has a rotation value on the Y axis is anything other than 0, you will see the entity “snap” to the starting value of the gesture. Learn how to fix this in Rotate Gesture 3D Improved.

Support our work so we can continue to bring you new examples and articles.

Download the Xcode project with this and many more examples from Step Into Vision.
Some examples are provided as standalone Xcode projects. You can find those here.

Questions or feedback?