Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rotating Skybox

Discussion in 'General Graphics' started by frasderp, Jun 22, 2020.

  1. frasderp

    frasderp

    Joined:
    Oct 6, 2016
    Posts:
    19
    I have recently created a skybox material, using shadergraph. Essentially, the top of this skybox sphere is day, and the bottom night, with transitions being sunrise and sunset.

    In the scene, I have created a "planet" with a lightsource that orbits the planet. What I want to achieve, is that the skybox also rotates with the lightsource, to represent day and night.

    I tried adding a camera to the lightsource, which achieves the effect of a rotating skybox, however (as you can guess) has no relation to day and night.
     
  2. frasderp

    frasderp

    Joined:
    Oct 6, 2016
    Posts:
    19
    Well I solved this by stacking cameras, and having the skybox camera rotate depending on the location of the sun and the player, related to each other.

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         player = GameObject.FindWithTag("Player");
    4.         planet = GameObject.FindWithTag("Planet");
    5.         distancePlayerToPlanet = Vector3.Distance(player.transform.position, planet.transform.position);
    6.         distanceSunToPlanet = Vector3.Distance(sunSource.transform.position, planet.transform.position);
    7.         minDistance = distanceSunToPlanet - distancePlayerToPlanet;
    8.         maxDistance = distancePlayerToPlanet + distanceSunToPlanet;
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.      
    15.         distancePlayerToSun = Vector3.Distance(player.transform.position, sunSource.transform.position);
    16.  
    17.         rotationAmount = (1-((distancePlayerToSun - minDistance) / (maxDistance - minDistance))) * -90;
    18.  
    19.         transform.localEulerAngles = new Vector3(rotationAmount, transform.localEulerAngles.y, transform.localEulerAngles.z);
    20.         }