Search Unity

Question Can't get the right rotation

Discussion in 'AR' started by mikkelsen1996, Nov 9, 2021.

  1. mikkelsen1996

    mikkelsen1996

    Joined:
    Sep 11, 2021
    Posts:
    5
    Hi.
    I'm making an AR app where a direction light is suppose to be placed at the position of the sun, in order to make the shadows of the objects look more realistic. I'm getting two angles, describing the position of the sun with respect to the North Pole, from an API. By checking the Input.compass.trueHeading I calculate the rotation which the directional light in the scene most make to be positioned correctly. However, the rotation of the directional light is incorrect. I have attached some images of the setup and the specific script in which most of the code is in, is below:



    Code (csharp):
    1.  
    2.  
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using UnityEngine;
    7. using IpGeoLocApiv2;
    8.  
    9. public class SetSunPosition : MonoBehaviour
    10. {
    11.  
    12.     float timer = 0;
    13.     public GameObject player;
    14.     public GameObject sun;
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         // How often do we update sun position
    20.         timer += Time.deltaTime;
    21.         if (timer >= 12)
    22.         {
    23.             UpdateSun();
    24.             timer = 0;
    25.         }
    26.     }
    27.  
    28.  
    29.  
    30.     private void UpdateSun()
    31.     {
    32.         int compass = (int)GPSTracker.north; // compass heading with respect to northpole
    33.         int sunAzimuth = (int)Sun_API.Sun_Azimuth; // Suns current azimuth with respect to the northpole
    34.         int angle;
    35.  
    36.         Console.WriteLine("sunAzimuth2: " + sunAzimuth);
    37.         Console.WriteLine("compass: " + compass);
    38.         if (compass > sunAzimuth)
    39.         {
    40.             angle = sunAzimuth + (360 - compass);
    41.         }
    42.         else
    43.         {
    44.             angle = sunAzimuth - compass;
    45.         }
    46.         float radian = angle;
    47.  
    48.  
    49.         Vector3 sunAngle = new Vector3()
    50.         {
    51.             x = sun.transform.localEulerAngles.x,
    52.             y = radian,
    53.             z = sun.transform.localEulerAngles.z,
    54.         };
    55.  
    56.         sun.transform.rotation = Quaternion.Euler(sunAngle);
    57.  
    58.       //  Console.WriteLine("sun angle after: x = " + sun.transform.localEulerAngles.x + " y = " + sun.transform.localEulerAngles.y + " y = " + " z = " + sun.transform.localEulerAngles.z);
    59.     }
    60. }
    61.  
     

    Attached Files: