I'm trying to make my SCNNode automatically rotate horizontally by itself. This is the code I have so far:
box.scale = SCNVector3(x: 0.26, y: 0.26, z: 0.26)
box.position = SCNVector3(0.15, 3.85, -3)
How do I make this box spin horizontally automatically? And for a plus, is there a way to make it happen with user interaction enabled too? Thanks!
Update: If I didn't sound as clear, this is what I mean: You know when you spin a basketball on your finger and how it's spinning horizontally? That's the effect I'm trying to achieve! Thanks!
You can either
SCNAction
class, e.g. SCNAction.rotate(by:around:duration:)
. This has the advantage that you can chain multiple animations or repeat them forever,Edit:
let action = SCNAction.repeatForever(SCNAction.rotate(by: .pi, around: SCNVector3(0, 1, 0), duration: 5))
box.runAction(action)
SCNTransaction
.You can change the transform like this
let oldTransform = box.transform
let rotation = SCNMatrix4MakeRotation(axis.x, axis.y, axis.z angle)
SCNTransaction.begin()
SCNTransaction.animationDuration = 0.5
box.transform = SCNMatrix4Mult(rotation, oldTransform)
SCNTransaction.commit()
To use user input you can for example change the angle of the rotation or the duration with a gesture recognizter