'How to Rotate ARKit Entity Towards the User's Screen/Camera

I have it set up so that the mesh I'm adding to my scene is rendering correctly (right side up so that the text I add as a child is legible), but the rotation is always the same (globally) whereas I want the rotation (on the XZ plane) to be towards the camera, but I'm not exactly sure how to go about this

My code looks like this:

@objc func handleTap() {
            guard let view = self.view else { return }
            
            guard let query = view.makeRaycastQuery(from: view.center, allowing: .estimatedPlane, alignment: .any) else { return }
            guard let raycastResult = view.session.raycast(query).first else { return }
            
            // set a transform to an existing entity
            var transform = Transform(matrix: raycastResult.worldTransform)
            
            // Create a new anchor to add content to
            if(oldAnchor != nil) {
                view.scene.removeAnchor(oldAnchor!)
            }
            let anchor = AnchorEntity()
            oldAnchor = anchor
            view.scene.anchors.append(anchor)
            let material = SimpleMaterial(color: .lightGray, isMetallic: false)

            // Add a curve entity
            let curveEntity = try! ModelEntity.loadModel(named: "curve")
            curveEntity.transform = transform
            curveEntity.transform.rotation = simd_quatf(angle: 0, axis: SIMD3<Float>(1, 1, 1))
            curveEntity.scale = [0.0002, 0.0002, 0.0002]
            
            let curveRadians = 90.0 * Float.pi / 180.0
            curveEntity.setOrientation(simd_quatf(angle: curveRadians, axis: SIMD3<Float>(1,0,0)), relativeTo: curveEntity)
            // Adding text and children and materials etc.
            ...
            anchor.addChild(curveEntity)
}

Without the curveEntity.transform.rotation = simd_quatf(angle: 0, axis: SIMD3<Float>(1, 1, 1)) line, the rotation of the curve is relative to the normal of the surface that gets raycasted upon, instead of constant.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source