Rotate object to look at camera using Realitykit Look(at) is broken

I am playing around in Realitykit and would like an Entity to always look at camera. I've seen similar questions on stackoverflow and developer forums and followed their suggestion to use look(at) method. I am aware that reality composer can achieve the same effect using behavior but I would later have to use the same technique to anchor an Arrow to the camera using AnchorEntity and have it look at another entity in the scene for user navigation.

I am using look(at) method to orient the entity to the camera right now by calling it in ARKit session Didupdate method, however, it is broken:
 
entity.look(at: arView.cameraTransform.translation, from: entity.position, upVector: [0,0,0], relativeTo: nil)

This will move my entity to a new position in the scene and since it is called at ARKit session Didupdate it will keep moving further and further away every frame. I thought it is not supposed to move since I've set the "from:" to object's current position so it always move to where it was and hence static. My entity is an AnchorEntity that act as a root entity for the model loaded from reality composer.
  • How can I add objects facing camera all the time on Reality Composer?

Add a Comment

Accepted Reply

after some investigation, I found the problem. You CANNOT anchor it to an anchor object, such as face, camera, plane. It messed up the position system!!! Instead, anchor it to the world origin and if you want to move it, just sets its transform every frame. this is not optimized but it is the only way this function will work!

Replies

after some investigation, I found the problem. You CANNOT anchor it to an anchor object, such as face, camera, plane. It messed up the position system!!! Instead, anchor it to the world origin and if you want to move it, just sets its transform every frame. this is not optimized but it is the only way this function will work!
I am back with the final solution!

The method above does work if you would like a floating pointer in front of your camera to show direction to an entity in scene, but, it is glitchy and if your hand's shaking slightly the pointer will also shake slightly because of the slight delay of getting the camera position and moving your pointer relative to that position!

The improved method, which work like a charm, is to create a custom placeholder entity that conforms to hasAnchoring, and has no children, no model, just a placeholder. You call the look(at) method on this placeholder entity which won't consume much processing power since it is empty. It will move the object and orients it correctly. Now, you get the orientation of this placeholder entity, apply to your real pointer, which is a child of an AnchorEntity anchored to camera, and set its orientation with respect to world coordinate. Boom! It worked! no more shaking! Don't forget to offset the pointer so it is in front of the camera.

A little bit of code for those came across this post in the future:

 pointer.setOrientation(placeHolder.orientation, relativeTo: nil)
  • @Mokin, can you share an example code of how we could implement your solution?

Add a Comment
Hello,

You are specifying an up vector of (0,0,0) in your look(at:from:upVector:relativeTo:)
call. Instead, you should use an up vector of (0,1,0) since you are working in world-space.
@Mokin, can you please provide a code snippet with your solution. I am having the same issue and cannot figure it out.
It seems like folks are still having issues with this:

Code Block
extension Entity {
    /// Billboards the entity to the targetPosition which should be provided in world space.
    func billboard(targetPosition: SIMD3<Float>) {
        look(at: targetPosition, from: position(relativeTo: nil), relativeTo: nil)
    }
}


Then subscribe to the scene update event and call billboard on your entity with the targetPosition:

Code Block
arView.scene.subscribe(to: SceneEvents.Update.self) { [self] _ in
      myEntity.billboard(targetPosition: arView.cameraTransform.translation)
 }.store(in: &cancellables)

  • This is a great solution. On thing I have noticed is that the entity seems to be facing backwards. Any idea on what would be causing this?

  • @eaallen, you can try switching the at and from arguments to the look function. Or you can try applying a 180 degree rotation along one of the axes after the look function.

  • yes I believe that would fix it. But is the problem caused by the model "face" being backwards from what I expected? so when I use look(at:from:relative) it appears to be backwards?