GKAgent3d wander goal to stop agent from flying around

I am trying to learn GameplayKit and am using it with SceneKit so I am using a GKAgent3d for my agent to set behaviours on. I have got behaviours working but I am trying to have the agent wander around a plane that I have representing ground but when the agent is wandering it seems to be wandering on all axis so it just ends up flying around. Is there any way to have the agent stick to the ground when it is wandering?

I have semi sorted it by setting constraints to the Y value by using the following code in the RenderComponent of the entity;

        let heightConstraint = SCNTransformConstraint.positionConstraint(inWorldSpace: false) { (node, position) in
            var constrainedPosition = position
            if position.y > 0 { constrainedPosition.y = 0; node.position.y = 0}
            if position.y < 0 { constrainedPosition.y = 0; node.position.y = 0}
            return constrainedPosition
        }

        node.constraints = [heightConstraint]

I am wanting to have these agents move around on a landscape so they won't be on straight flat ground, my hope was to find a way of having the update routine for the node perform a raycast to check the height of the ground below it so it could move up and down and not be constrained to a static height but the node raycast only returns child objects of the node which the ground would not be so I have to come up with a new method if there is one.

I am just documenting my discoveries in case it is of use to anyone else but I managed to perform a raycast from the node so that it detects things other than its child nodes, I run this function on update but I am not sure if there is a more efficient way of doing it.

func heightTest(){
        let result = sceneNode?.hitTestWithSegment(from: sceneNode!.position, to: SCNVector3(x: sceneNode!.position.x, y: -100, z: sceneNode!.position.z), options: [SCNHitTestOption.rootNode.rawValue: scene.rootNode])

        if let intersection = result?.first?.simdWorldCoordinates {
            print(intersection)
        }

    }
GKAgent3d wander goal to stop agent from flying around
 
 
Q