Search code examples
iosswiftscenekitarkit

Scenekit move object with respect to camera


I've placed a node in Scenekit/ARKit

Now I want to move node/object to right and left with help of buttons. Issue is when I moves my camera/device and press right button, node moves in direction of axis it was first initiated, Not moves right with respect to camera. How can I achieve this?

current code on tapping right button

position.x += 0.2

Solution

  • Here is the solution

    guard let front = sceneView.pointOfView?.simdWorldFront else {return}
    let horazontalRight = cross(front, simd_float3(0,1,0))
        
    // for moving right
    node.position +=  SCNVector3(horazontalRight *  0.01)