Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question How can I make the placed model always face the camera?

Discussion in 'AR' started by cagrigecin, Jun 30, 2022.

  1. cagrigecin

    cagrigecin

    Joined:
    Apr 27, 2022
    Posts:
    2
    Hi, I want to place my model and always face to the camera of the phone. I tried to calculate the angle between the camera and the placed object in the x and z axis and then according to this angle I want to rotate my object in the y axis but I didn't get correct results from that process. I tried to figure out that which angle should I use but none of them seemed correct to me.

    Code (CSharp):
    1. if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    2. {
    3.    if (m_RaycastManager.Raycast(Input.GetTouch(0).position, s_Hits, TrackableType.PlaneWithinPolygon))
    4.    {
    5.        var firstHit = s_Hits[0];
    6.        var camPosition = Camera.main.transform.position;
    7.  
    8.        //I tried to calculate angles
    9.        var angleHitRelativePoseToCam = Mathf.Atan2(firstHit.sessionRelativePose.position.z-camPosition.z, firstHit.sessionRelativePose.position.x-camPosition.x) * Mathf.Rad2Deg;
    10.  
    11.        var angleHitRelativePoseTo0 = Mathf.Atan2(firstHit.sessionRelativePose.position.z-0, firstHit.sessionRelativePose.position.x-0) * Mathf.Rad2Deg;
    12.  
    13.        var angleHitPoseTo0 = Mathf.Atan2(firstHit.pose.position.z-0, firstHit.pose.position.x-0) * Mathf.Rad2Deg;
    14.  
    15.        var angleHitPoseToCam = Mathf.Atan2(firstHit.pose.position.z-camPosition.z, firstHit.pose.position.x-camPosition.x) * Mathf.Rad2Deg;
    16.  
    17.       modelContent.transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
    18.       m_SessionOrigin.MakeContentAppearAt(modelContent, firstHit.pose.position);
    19.  
    20.       //one of the attepms
    21.       m_SessionOrigin.MakeContentAppearAt(modelContent, firstHit.pose.position, m_Rotation); //0 0 0
    22.  
    Then I tried to use
    transform.LookAt()
    method between object, camera and sessionOrigin.

    modelContent.transform.LookAt(m_SessionOrigin.transform);

    At the end I didn't achieve any result from those tries.

    So any help would be much appreciated!! Thanks
     
  2. ankur-unity

    ankur-unity

    Unity Technologies

    Joined:
    Aug 12, 2021
    Posts:
    34
  3. cagrigecin

    cagrigecin

    Joined:
    Apr 27, 2022
    Posts:
    2
    Thank you for your reply, I also tried this approach but It is rotating my model on X and Z axis, I just want to rotate it on Y axis. I tried to get Y rotation value and set it to the model as new Y rotation from the
    LookAt()
    method but this also didn't work for me. I think there is relative coordinate system issue that I can't find correct inputs for my problem. I mean my placed object could has plane related coordinates and my camera gives me another coordinate space input and I can't calculate correct angle.