Search Unity

Question Rotating directional light in AR scene

Discussion in 'AR' started by mikkelsen1996, Dec 13, 2021.

  1. mikkelsen1996

    mikkelsen1996

    Joined:
    Sep 11, 2021
    Posts:
    5
    I'm trying to rotate a directional light to match the position of the real sun. To do so I'm using the trueHeading variable from the compass of the phone, and an API which reads the position of the sun with respect to the current location. All of this values have been tested and they all seems to be correct. However, when I try to rotate only around y-axis a rotation is also made on the x-axis, and also the rotation does not seem to be right for the y-axis.
    The prints for all the variables both before and after the rotation looks like this, and the corresponding images can be seen below:

    First values:
    Compass = 356,0083

    Before rotation:
    PlayerYBefore = 356,0083

    SunXBefore = 45

    SunYBefore = -1,207418E-06

    SunZBefore = 157,02


    After rotation:
    PlayerYAfter = 356,0083

    SunXAfter = 2,762822

    SunYAfter = 44,04689

    SunZAfter = 351,0968




    Second values:

    Compass = 40,28704

    Before rotation:
    PlayerYBefore = 40,28704

    SunXBefore = 2,762822

    SunYBefore = 44,04689

    SunZBefore = 351,0968

    After rotation:


    PlayerYAfter = 40,28704

    SunXAfter = 357,0963

    SunYAfter = 349,5582

    SunZAfter = 343,696
    263331605_233157175598164_928870227549831470_n.jpg Default placement


    262763763_1035444057033802_7516113406827843114_n.jpg First rotation



    261759706_924965811747495_7384508582258070474_n.jpg Last rotation

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using IpGeoLocApiv2;
    7.  
    8. public class SetSunPosition : MonoBehaviour
    9. {
    10.  
    11.     float timer = 0;
    12.     public GameObject player;
    13.     public GameObject sun;
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         // How often do we update sun position
    19.         timer += Time.deltaTime;
    20.         if (timer >= 12)
    21.         {
    22.             UpdateSun();
    23.             timer = 0;
    24.         }
    25.     }
    26.  
    27.  
    28.  
    29.     private void UpdateSun()
    30.     {
    31.         int compass = (int)GPSTracker.north; // compass heading with respect to northpole
    32.         int sunAzimuth = (int)Sun_API.Sun_Azimuth; // Suns current azimuth with respect to the northpole
    33.         int sunAltitude = (int)Sun_API.Sun_Altitude; // Suns current altitude
    34.         int angle;
    35.  
    36.        
    37.         if (compass > sunAzimuth)
    38.         {
    39.             angle = sunAzimuth + (360 - compass);  
    40.         }
    41.         else
    42.         {
    43.             angle = sunAzimuth - compass;
    44.         }
    45.         int radian = angle;
    46.  
    47.        
    48.         Vector3 sunAngle = new Vector3()
    49.         {
    50.             x = sun.transform.rotation.eulerAngles.x,
    51.             y = radian + player.transform.rotation.eulerAngles.y,
    52.             z = sun.transform.rotation.eulerAngles.z,
    53.         };
    54.  
    55.         Console.WriteLine("Compass : ");
    56.         Console.WriteLine(player.transform.rotation.eulerAngles.y);
    57.         Console.WriteLine("PlayerYBefore : ");
    58.         Console.WriteLine(player.transform.rotation.eulerAngles.y);
    59.  
    60.         Console.WriteLine("SunXBefore : ");
    61.         Console.WriteLine(sun.transform.rotation.eulerAngles.x);
    62.         Console.WriteLine(" SunYBefore : ");
    63.         Console.WriteLine(sun.transform.rotation.eulerAngles.y);
    64.         Console.WriteLine(" SunZBefore : ");
    65.         Console.WriteLine(sun.transform.rotation.eulerAngles.z);
    66.  
    67.         sun.transform.Rotate(sunAngle);
    68.  
    69.         Console.WriteLine("PlayerYAfter : ");
    70.         Console.WriteLine(player.transform.rotation.eulerAngles.y);
    71.  
    72.         Console.WriteLine("SunXAfter : ");
    73.         Console.WriteLine(sun.transform.rotation.eulerAngles.x);
    74.         Console.WriteLine(" SunYAfter : ");
    75.         Console.WriteLine(sun.transform.rotation.eulerAngles.y);
    76.         Console.WriteLine(" SunZAfter : ");
    77.         Console.WriteLine(sun.transform.rotation.eulerAngles.z);
    78.  
    79.     }
    80. }
    81.