- Home /
Mathf.PingPong float values
This appears like it would be easy but it is racking my brain. What I am trying to do is use the skybox blender shader to transition between two different skyboxes. This I have working correctly. I have the skyboxes successfully transition when the float blend goes from 0 to 1. But I am having trouble with getting the float to, after it completes this initial transition, to then lerp back from 1 to 0. Here is an example of the script:
public class DayNight : MonoBehaviour {
public float blend;
public Color daycolor = new Color (0.95f, 0.66f, 0.4f, 1.0f);
public Color32 nightcolor = new Color (0.13f, 0.26f, 0.44f, 1.0f);
void Start ()
{
RenderSettings.ambientLight = daycolor;
}
void Update ()
{
blend = Mathf.Lerp(0, 1, Time.time*.05f);
RenderSettings.skybox.SetFloat("_Blend", blend);
RenderSettings.ambientLight = Color.Lerp (daycolor, nightcolor , Time.time*.05f);
}
}
How do you use, or can you use, Mathf.PingPong to ping pong between float values? All I ever see is it using a vector3.
Thanks
Answer by robertbu · Dec 09, 2013 at 09:40 PM
Mathf.PingPong only does float values. So your code might look like:
blend = Mathf.PingPont(1.0f, Time.time*.05f);
Might also consider using Sin() for a smoother transition:
blend = (Mathf.Sin(Time.time * speed) + 1.0f) / 2.0f;
'speed' is a variable you define or a constant you insert.
Thanks for the quick reply. It is working correctly, the $$anonymous$$athf.Sin one, as far as what I want to happen except that I doesn't transition from 0 to 1, it stops about half way there. I have managed to have it transition from -1 to 1 but I need it to go from 0 to 1 and then back from 1 to 0. Any direction about how to do this?
Again, thanks.
Never $$anonymous$$d, I figured it out.
Thanks a lot for your help
the second one worked perfectly in edit mode. Thank you!