Search Unity

ARFoundation - Calculate cam height above detected plane for the placed prefab.

Discussion in 'Handheld AR' started by newguy123, Jan 30, 2019.

  1. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    157
    Hi Team

    I'm struggling to calculate the height.
    I want to set an if statement to do something, depending on how high the camera is above the plane. Meaning, only the y value.

    I have a float "myTempHeight"
    and I calculate it like so:
    myTempHeight = Mathf.Abs((hitPose.position - m_SessionOrigin.transform.position).y);


    ...however, something tells me its not working, since no matter how high I hold the cam from the plane, it always results in the same condition.

    Am I doing the height calculation correct?
     
  2. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    157
    I've also tried making a temp Vector 3 and calculating Vector3.Distance, but still doesnt work:

    Code (CSharp):
    1. Vector3 tempTransform = hitPose.position;
    2. myTempHeight = Vector3.Distance(tempTransform, new Vector3(tempTransform.x, m_SessionOrigin.transform.position.y, tempTransform.z));
    3.  
    What am I doing wrong?
     
  3. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    157
    I've now added some UI text in there so I can see the values.

    Seems at the point when you tap to place the prefab, the hitpose and m_SessionOrigin are at exactly the same coordinates so they will always equal each other so my answer above will always be 0.

    This doesnt make sense to me currently. Anybody have any clues here?

    The cam is clearly above the hitpose as I am above my prefab as I'm looking down at it...

    *confused*
     
  4. BuoDev

    BuoDev

    Joined:
    Nov 28, 2018
    Posts:
    42
    The ARSessionOrigin m_SessionOrigin is the Unity origin which becomes the camera's position when tracking starts.

    Try getting the first hit result in ARRaycastHit, which is the first trackable [0] that your finger hit.
    Pose trackablePose = touchHits[0].pose;

    Then get its position.
    Vector3 trackablePosition = trackablePose.position;
     
  5. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    157

    Yes I already have that. What I dont understand is how to get the cam height. Can't I just get the m-session's y position?
    Od do I have to findgameobjectwithtag("MainCamera") and get that y instead

    Then compare the cam's y with the hitpose's y?
     
  6. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    518
    The calculation in your initial post looks correct to me.

    What tells you its not working? What is the "condition" that is always the same?

    Are you sure? The default
    ToString()
    for
    Vector3
    s only prints a single digit after the decimal place, so if you're printing them to screen or console, then they may look like the same value if they are within 10 centimeters of each other.
     
  7. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    157
    Yes like I said the 2 values equaled each other when using 2 decimal values ("F2")

    I ended up using the cam's y and hitpose's y (which makes no sense since the cam starts at 0,0 just like the ARSessionOrigin)

    Anyway, from a quick test the following code APPEARS to work, although I would need to do more extensive testing to be sure it does what I intended:

    So inside the update inside the touch = input if statement, among other thngs I have:

    Code (CSharp):
    1. Vector3 tempTransform = hitPose.position;
    2.  
    3. ActualHeight = Vector3.Distance(tempTransform, new Vector3(tempTransform.x, theCam.transform.position.y, tempTransform.z));
    4.  
    5.                     if (ActualHeight < 0.8f)
    6.                     {
    7.                         m_SessionOrigin.transform.localScale = new Vector3(500f, 500f, 500f);
    8.                     }
    9.                     else
    10.                     {
    11.                         m_SessionOrigin.transform.localScale = new Vector3(175f, 175f, 175f);
    12.                     }
    The more complicated than normal Vector3.Distrance trick there works on only 1 axiz instead of all 3 which is what I want in this case
     
  8. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    518
    I'm sorry, I think I misread your initial post. The session origin isn't moving (unless you move it), but the camera is what is driven by the device pose, and from where the ray originates, so using the camera's Y value is correct.
     
    newguy123 likes this.
  9. BuoDev

    BuoDev

    Joined:
    Nov 28, 2018
    Posts:
    42
    Actually, I think it makes perfect sense. The ARSessionOriginin can be absolutely anywhere. If you started tracking at 1m height, thats close to where the ARSessionOrigin's 0,0,0 point would be. It's pretty much impossible for the ARSessionOrigin to start at the floor height, so calculating the height of an object shouldn't use the ARSessionOrigin's position at all. Especially since planes can be at different heights, it isn't a good reference point.