Search code examples
iosswiftscalingarkitscnnode

How to scale 3D object vertically (along y-axis) in Swift?


I created a door with ARKit and I want create a scale animation. My goal is to scale it only along the y-axis (stretch the door to be longer). I want the door to grow within a duration of 1 second.

My approach was to simply scale it but I only have options that allow me to scale my entire object along all 3 axis.

Next I tried node.scale = SCNVector3(0, 2, 0) and that works ok, but it has no nice animation to it. When I create a SCNAction() and run the code as a block with duration time, it still just changes the size without any smooth animation.


Solution

  • You need to use SCNTransaction. The simplest way to animate your node scaling would be something like this:

    SCNTransaction.begin()
    SCNTransaction.animationDuration = 3
    node.scale = SCNVector3(0, 2, 0)
    SCNTransaction.commit() 
    

    You might also need to modify pivot property of the node to position the animation correctly.