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. Dismiss Notice

Sun Shafts - Use 'Centre On Main Camera' through script at runtime?

Discussion in 'Scripting' started by JanDawid, Oct 2, 2016.

  1. JanDawid

    JanDawid

    Joined:
    Jul 7, 2014
    Posts:
    283
    I have a system that uses a main scene (camera, directional light, character, etc.) which is loaded from the title screen, and then loads additive scenes (levels), and as you go between levels it just loads more additive scenes and hence uses this one main scene the whole time without unloading it/loading in new cameras and what not.

    Anyway, in each different area, the sun will be in a different position (think Dark Souls, open world but each area is a different type of day), and I'd like the sun shafts to always be coming out of the directional light (Sun). I can achieve this currently in the editor if I angle the camera a certain way, pause the game window and click 'centre on main camera'.

    My problem is quite clear; I cannot set this in script and this needs to be something I can change at will during runtime. Am I not seeing the proper variable from the SunShafts.cs script? Or is there another way to do this?

    Thank you!
     
  2. JanDawid

    JanDawid

    Joined:
    Jul 7, 2014
    Posts:
    283
    Bump because it's already pushed off the front page of Scripting. Sorry.
     
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Did you try changing the rotation of the directional light? If so what happened?

    Code (CSharp):
    1. // Drag the DirectionLight from the Scene onto this variable
    2. GameObject mainLight;
    3.  
    4. //Then in whatever function loads the new Scene
    5. // Change the values of the direction light.
    6. mainLight.transform.rotation = Quaternion.Euler(xRot,yRot,Zrot);
    Should be able to load a particular scene, and in the Editor drag the X,Y,Z rotations of the light around till you get the direction you like that looks good, write those numbers down , and save them all in a const float array somewhere. Load them up into your light as you need.
     
    JanDawid likes this.
  4. JanDawid

    JanDawid

    Joined:
    Jul 7, 2014
    Posts:
    283
    That's the thing; despite the SunShafts.cs script having a set caster (directional light) it needs to be hit with that 'Centre on main camera' function to change the origin of the sun shafts. Rotating the directional light only moves the sun in the sky, but the shafts still emit from the same location. Like I said, it needs to be centred on the main camera to change the location the shafts are emitted from.
     
  5. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    Center on main camera either performs a Quaternion.LookAt(Camera.main.position) or it does transform.posotion = Camera.main.transform.position; transform.rotation = Camera.main.transform.rotation; Try either of those.
     
  6. JanDawid

    JanDawid

    Joined:
    Jul 7, 2014
    Posts:
    283
    Well what would I be rotating? The directional light doesn't affect the sunshafts themselves, as they have to be a component for the main camera. And changing the camera's rotation doesn't change anything anyway.

    Come to think of it, I don't need to run the 'Center on main camera' function, but actually just a 're-focus on the shafts caster'.

    It's like something that should be accessible in the SunShafts.cs script just isn't. I can move around the directional light and camera all I want, but the Sun Shafts just remain where they were placed before run time no matter what.

    Hitting the 'Center on main camera' button is the ONLY thing that I can find that changes the position of the sun shafts origin at all! There should be:

    A. A way to activate this function in the script.

    B. Have the origin of the sun shafts update with the direction of the shafts caster. Which is basically what I want.

    It's like Unity programmed this with only fixed light sources in mind. :p
     
    Last edited: Oct 4, 2016
  7. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Heh, I hadn't realized Sun Shafts was a built in Unity thing! :) Just reading up on it now, it appears the position of the light source is significant for the Sun Shafts, which normally for a directional light makes no difference. So you can move the directional light around and it won't change how your scene is lit. After you hook up your directional Light to the sun shaft, try moving its X,Y,Z position around manually and see how that affects the sun shaft look. Then in script you can do basically what I mentioned before, but instead of changing the rotation of your directional light, change its transform position on a Scene Load
     
    JanDawid likes this.
  8. JanDawid

    JanDawid

    Joined:
    Jul 7, 2014
    Posts:
    283
    Oh! That did the trick; had barely noticed any difference when moving the position before, must've not moved it far enough. Yeah, so looks like I'll have to set a different position for each scene, rather than a different rotation.

    I guess I'll also have to get the directional light to look at the origin (or whichever way) to get the rotation to line up the sun with the new position of the shafts.

    Anyway, thanks for all the help! :D
     
    takatok likes this.