Using iOS14.2, XCode12.1, Swift5.3,
My first AR experience in my Xcode project works nicely.
In reality composer, I defined an anchor of type "image" and placed a 3D-object on its XY-center. In addition I added a small behaviour animation that makes the 3D-object flip when touched and send a touch-notification event right away. It all works very nicely in my App.
However, I would like to do some next steps and don't know how to achieve this.
In particular: step nr 3 in the below list I don't know how to do:
- tap on an AR-object and make it flip
- make the object disappear after the flip-animation
- make the object re-appear once the camera moves again over the anchored-image (or anchor-object)
The code for step 1 and 2 can be found below.
The problem I see is that the object never re-appears - even tough the camera points to the anchor-image.
Is there any way where you make a 3D object disappear (or hide) by touching it and make it re-appear once you move your camera towards the anchor-image (or anchor-object) again ??
import UIKit
import RealityKit
class MySceneViewController: UIViewController {
@IBOutlet weak var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let myScene1Anchor = try! MyScene1.loadScene()
// arView.scene.addAnchor(myScene1Anchor)
arView.scene.anchors.append(myScene1Anchor)
// Register an onAction handler for the notification
// action "tearTouched" you created in Reality Composer
myScene1Anchor.actions.tearTouched.onAction = handleTapOnEntity(_:)
}
func handleTapOnEntity(_ entity: Entity?) {
guard let entity = entity else { return }
// this is the callback that kicks in once the touch and flip-animation has happened:
// the object can be removed like this
// !!!!!!!!!!!!! However, after removing, the object will never appear again.
entity.removeFromParent()
// ????????????? How can I make this disappearing better so that the object re-appears after the camera shows the anchor-image again ??????????
}
}