Sachin
Sachin

Reputation: 105

Adding animation to 3D models in ARKit

In this video an object is given an animation to hover around the room when placed and then when tapped it gets dropped down with another animation. How can I add this kind of animations in my project? Is adding an already animated object the only way?

Thank you

https://youtu.be/OS_kScr0XkQ

Upvotes: 1

Views: 4824

Answers (1)

Clay
Clay

Reputation: 1761

I think the hovering up and down is most likely an animation sequence. The following animation sequence for a hovering effect will work inside the first drop down selection function.

        let moveDown = SCNAction.move(by: SCNVector3(0, -0.1, 0), duration: 1)
        let moveUp = SCNAction.move(by: SCNVector3(0,0.1,0), duration: 1)
        let waitAction = SCNAction.wait(duration: 0.25)
        let hoverSequence = SCNAction.sequence([moveUp,waitAction,moveDown])
        let loopSequence = SCNAction.repeatForever(hoverSequence)
        node2Animate.runAction(loopSequence)

        self.sceneView.scene.rootNode.addChildNode(node2Animate)

The second part to stop the animation when you tap the node put this inside the tap gesture function.

node2animate.removeAllActions()

The last part dropping to the floor, the node2animate might be a physicsBody and before the tap the gravity attribute

node2animate.physicsBody?.isAffectedByGravity = false

after the tap you set it to true

node2animate.physicsBody?.isAffectedByGravity = true

There is other stuff going on as well, collision with the floor have also been set etc.

Upvotes: 12

Related Questions