SceneKit: How to constrain movement of a node to one axis locally, not the entire world

It’s very easy to prevent movement of a node in one axis (X, Y, or Z) being moved by other forces with an SCNTransformConstraint.positionConstraint in world space as follows:

let zPosition = node.position.z

let strafingConstraint = SCNTransformConstraint.positionConstraint(inWorldSpace: true, with: {(_ node: SCNNode, _ position: SCNVector3) -> SCNVector3 in
         localPosition = position
         localPosition.z = zPosition
         return localPosition
})

node.constraints = [strafingConstraint]

However, this constrains this in world space. What do I do if I want to just make the node no longer move in its local Z axis, regardless of its orientation, so it can only move in its local X and Y axes, what do I do? Is there a way to use the .convertPosition(:from:) or .convertPosition(:to:) functions to do this? Is there a better way?

This topic was automatically closed after 166 days. New replies are no longer allowed.