Jerry Lee
Jerry Lee

Reputation: 863

RealityKit – Dragged Object Switches Planes

How to develop this function?

When the user drags the ModelEntity, the ModelEntity can switch planes.

Look at this demo video.

Upvotes: 1

Views: 448

Answers (1)

Andy Jazz
Andy Jazz

Reputation: 58553

You should use ray-casting instance methods to get the desired behavior:

@IBOutlet var arView: ARView!

guard let raycastQuery = arView.makeRaycastQuery(from: arView.center,   // CGPoint
                                             allowing: .estimatedPlane, // Target
                                            alignment: .horizontal)     // TargetAlignment
else { return }

// This method checks once for intersections between a ray and real-world surfaces
guard let raycastResult = arView.session.raycast(raycastQuery).first
else { return }

let worldTransform = Transform(matrix: raycastResult.worldTransform)    // simd_float4x4

let anchor = AnchorEntity(raycastResult: raycastResult)
// yourModel.transform = worldTransform
// or...
// anchor.transform = worldTransform

anchor.addChild(yourModel)
arView.scene.anchors.append(anchor)

Upvotes: 3

Related Questions