Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
2.98k stars 1.11k forks source link

[Bug] Raycasting on vertical plane returns point with strange normal rotation on device (in editor xr env is ok) #1045

Closed artyomabgaryan closed 1 year ago

artyomabgaryan commented 1 year ago

Describe the bug Casting rays against vertical plane returns steady point in space in UNITY Editor, but on device it returns very strange result which you can see on video below Editor: https://www.dropbox.com/s/4nrgqpg1gclx847/Unity_U8mEKylotL.mp4?dl=0 Device: https://www.dropbox.com/s/5wqeq5fwbzo6yvh/Screenrecorder-2022-12-24-21-12-55-294%280%29.mp4?dl=0

To Reproduce Steps to reproduce the behavior:

  1. Download the project ARraycastBug.zip
  2. Open it, switch to android platform
  3. Open scene Assets/ExampleAssets/Scenes/SampleScene.unity
  4. Optional: (Enable XR Environment) find a vertical plane (wall) and notice how axis gameobject steadily stick to wall
  5. Make a build on android device, find a vertical plane (wall) and notice how axis gameobject being rotated while you move device from side-to-side

Expected behavior In Editor you'll getting "steady" rotation of hit point after raycast Raycast returns result in editor (editor link above)

Actual behavior On device I'm getting strange rotation of hit point after raycast Raycast returns result like in video above (device link above)

Smartphone (please complete the following information):

tdmowrer commented 1 year ago

I believe this is the expected behavior on Android.

In AR Foundation, the normal points along +Y while the X and Z axes are parallel to the surface. However, there are many choices for X and Z and it is up to the underlying AR backend to determine what they are. All choices are equally valid.

From the ARCore docs:

ArPlane : X+ is perpendicular to the cast ray and parallel to the plane, Y+ points along the plane normal (up, for AR_PLANE_HORIZONTAL_UPWARD_FACING planes), and Z+ is parallel to the plane, pointing roughly toward the user's device.

Therefore, the pose of the hit point rotates as you move because they have chosen Z+ such that it always points toward your device.

If having a "steady" rotation is important to you, you can construct one by using the hit normal and world up, e.g., using Quaternion.LookRotation:

// Note: only works for vertical surfaces since
// horizontal surface normals are colinear with world up.
var rotation = Quaternion.LookRotation(forward: hit.pose.up);
artyomabgaryan commented 1 year ago

Well thank you so much, Tim. This helped me to solve my problem, but as I said the behavior is very strange. Thanks again.