Does anyone know how I can rotate a skybox so it matches the position of my directional light?
I don’t want to animate it just rotate it into the correct position. It’s important that it matches the directional light rather than the other way around.
This is something that I wonder since long time why no engine allows a parameter of skybox rotation.
It should be so easy to do, I think, and so useful!
I place the light with the right direction only to find then that I have to rerender the skybox or make another one entirely because I can’t adjust the original one.
I found myself again recently in the same condition. Luckily I have my solution to create skyboxes in Max very easily so it wasn’t a problem but I guess not everybody has the knowledge or time to do this so an option to rotate the skybox would be definitely useful.
Maybe I’m missing something but since the skybox is composed of separate images right left etc. why don’t you just copy the images into the orientation you want and rotate the overhead one?
Add 6 Unity plane objects. Form them into a cube with the normals pointing inward. Parent planes to GameObject (preferably with GameObject in the center of the cube).
Make six Unlit materials, one for each face of the cube. Make materials for the six textures that make up the skybox.
Apply the materials to the skybox in such a way that, when looking at it from the inside, it looks like a skybox.
Make a new layer called “Skybox”. Add your fake skybox to that layer.
Make a new camera. Set it to only render the Skybox layer. Set its depth below the depth of your main camera.
Make sure your main Camera does not render the Skybox layer.
Position the new camera in the center of your fake skybox.
Write a script to make the new camera copy the rotation of your main camera.
You should now have a fake Skybox. If you want to rotate the skybox by itself, you can just rotate the GameObject.
Yeah that’s the first solution I thought of, Screenhog, thank you anyway, but the best solution, seeing that we already have a skybox built-in Unity, would be from them to add this option, right?
then make an GameObject to put all my components in the scene in including my cam.
Now I can rotate that object and the skybox background changes but the game setup is still the same.
you may have to change the scrips to handle this rotation.
Hang on, there is a much simpler solution than this. It’s always best to use the built in stuff if you can, and in this case it’s not too hard.
Heres what you can do:
Add a second camera named “SkyboxCamera” to whatever game object your “main camera” is attached to.
Set the clear flags of your Main camera to “don’t clear” in the inspector
Set the clear flags of “SkyboxCamera” to be “skybox”, and set the depth of “SkyboxCamera” to be 1 less than the depth of "MainCamera in the inspector, and the culling mask of “SkyboxCamera” to be “nothing”
add a script like below (or your own customized version) to your game object
set the MainCamera and SkyCamera variables of the script in the inspector
create your Unity Skybox and attach it to the “SkyboxCamera” in the inspector
set the rotation of the script in the inspector if you need to rotate your skybox only intially, otherwise you set it in an update function if you want the skybox to rotate while running
Good luck!
public class SkyboxCamera : MonoBehaviour {
// set the main camera in the inspector
public Camera MainCamera;
// set the sky box camera in the inspector
public Camera SkyCamera;
// the additional rotation to add to the skybox
// can be set during game play or in the inspector
public Vector3 SkyBoxRotation;
// Use this for initialization
void Start()
{
if (SkyCamera.depth >= MainCamera.depth)
{
Debug.Log("Set skybox camera depth lower "+
" than main camera depth in inspector");
}
if (MainCamera.clearFlags != CameraClearFlags.Nothing)
{
Debug.Log("Main camera needs to be set to dont clear" +
"in the inspector");
}
}
// if you need to rotate the skybox during gameplay
// rotate the skybox independently of the main camera
public void SetSkyBoxRotation(Vector3 rotation)
{
this.SkyBoxRotation = rotation;
}
// Update is called once per frame
void Update()
{
SkyCamera.transform.position = MainCamera.transform.position;
SkyCamera.transform.rotation = MainCamera.transform.rotation;
SkyCamera.transform.Rotate(SkyBoxRotation);
}
}
I had to make a update function like this to make it work (Space.World)
public function LateUpdate() : void
{
transform.position = parentTransform.position;
transform.rotation = parentTransform.rotation;
transform.Rotate ( 0.00, skyRotation, 0.00, Space.World);
}
also some tips;
I replaced transform with rTransform and added in awake rTransform = transform;
I renamed lateupdate() and call it from my actual camera script that provides the position and rotation to the function.
When using Deferred lighting, this does not seem to work,
the skybox won’t render, and if i change the camera witch has the depth only from deferred to forward it works.
Bug?
Also the skybox only cam can be set to vertex lit for performance or does this not matter?
I know this thread’s been running for a while, but here’s a real simple way to do it:
Do the double camera set-up as scottm 361 explained above, but then put this simple script on the SkyboxCam :
void FixedUpdate (){
transform.Rotate (0, rotation speed, 0);
}
Works great and hardly any typing at all.
Nic,
Your shader works well but it only seems to rotate the skybox about 2 axes. “Z” has no effect.
Strangely, the main camera y and z rotation have exactly the same effect.
Help please.