- Home /
Fading between skyboxes
I'm currently working on a starfox-style game. The idea is for the player to play through all levels without going back to a menu. Thus I want to swap out the skybox and lighting at certain points of the game. I've managed to quite simply swap the skybox thus far, but what I really want is to have a smooth fading effect.
My code thus far looks like:
using UnityEngine;
using System.Collections;
public class NewLevel : MonoBehaviour {
public Material material1;
public Material material2;
public float duration = 2.0F;
private bool newlevel= false;
void Start() {
newlevel = false;
}
void OnTriggerEnter(Collider other)
{
newlevel = true;
}
void Update() {
if (newlevel = false) {
RenderSettings.skybox= material1;
}
if (newlevel = true) {
float lerp = Mathf.PingPong (Time.time, duration) / duration;
RenderSettings.skybox.Lerp (material1, material2, lerp);
Debug.Log ("Skybox should be changing right about now");
}
}
}
The thing is that thus far it does nothing. In fact, as soon as I start the game, the debug message pops up, despite nothing actually colliding with the box. Any suggestions?
The fact that your debug message pops up indicates there is something wrong with your collision detection. Fix that first, then look at @mouurusai's answer.
Answer by mouurusai · Sep 28, 2014 at 09:58 PM
Material.Lerp- do not deal with textures. You should write/find shader for that.
$$anonymous$$aterial.Lerp will only switch the colours and floats. It won't actually switch between two shaders or textures. Write/find a single shader that takes a float parameter to switch between two textures. Then lerp that float.
If you needed to run more then two skyboxes you could still use the same shader, just swap the texture that isn't shown out at runtime.
Your answer
![](https://koobas.hobune.stream/wayback/20220613163926im_/https://answers.unity.com/themes/thub/images/avi.jpg)