I must be going blind, but this is not working for some reason? Code (csharp): using UnityEngine; using System.Collections; public class fogcontrol : MonoBehaviour { public float speed = 2.0f; private bool fading; private GlobalFog fogCtrl; private float fade=3.0f; void Start () { fogCtrl=GetComponent<GlobalFog>(); } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)) fading = true; if(fading) { fade= Mathf.Lerp(40.0f, 0.35f, Time.deltaTime * speed); fogCtrl.startDistance=fade; } if(fogCtrl.startDistance <0.4f) fading=false; print (fade +" "+fading); } }
Your code can't work like that; the third parameter in Lerp is a float between 0 and 1. If it's 0, then the first parameter is returned, if it's 1 then the second parameter is returned, and if it's between 0 and 1 then a proportional mix of the first and second parameters are returned. Since Time.deltaTime * speed is probably a quite small number, the result in this case will always be close to 40.0. --Eric
I used the same method in a JS script and there is works? I thought the 3 param was kind of the speed to interpolate between the two? so i should make a counter form 0 to 1? is that the right approach?
It could never work like that in any language; you did something differently. Nope, see the docs. Lerp is a simple math function (specifically, (1 - c)*b + c*a, where c is clamped between 0 and 1) that returns a value immediately; it doesn't run over time or anything. Yes. Coroutines are helpful for this, rather than using Update. --Eric
thanks Eric this is the JS: Code (csharp): #pragma strict var CLights:Light[]; var intensity=1.0; var fog_Density=0.5; var fadingSpeed=2.0; var fading=false; function OnTriggerEnter (collider:Collider) { for(var L in CLights) L.light.intensity=intensity; RenderSettings.fogDensity=0.01; fading=true; } function Update () { if(fading) { var fade=Mathf.Lerp(CLights[0].light.intensity, 0.0, Time.deltaTime*fadingSpeed); for(var L in CLights) L.light.intensity=fade; var fogIncrease=Mathf.Lerp(RenderSettings.fogDensity, fog_Density, Time.deltaTime*fadingSpeed); RenderSettings.fogDensity=fogIncrease; } if(fade<0.1) fading=false; }
The JS function works because CLights[0].light.intensity serves as your accumulator. You modify it during every update. In the first script you have no accumulator. Also, I would point out that your JS function is not going to fade linearly. It is going to slow down as it approaches black.
ok thanks for the explanation, i guess i had the wrong idea about Lerp although i read and understand the docs and the unity answer reply from eric before... is there a function that does (from: float, to: float, speed:float) ? or do i just use coroutines for these always?
Hey Guys, I would add that, if you are doing lots of Lerping, or you would like to extend functionality (like pausing), making a class around Lerp will get you a lot of mileage. A barebones example: Code (csharp): using UnityEngine; using System.Collections; public class Lerper{ bool _isLerping = false; public bool IsLerping {get {return _isLerping;} set{}} bool _isPaused = false; public bool IsPaused {get {return _isPaused;} set{}} float _lerpVal; public float LerpVal {get {return _lerpVal;} set{}} float accumulator = 0f; public IEnumerator Lerp(float start, float finish, float rate = 1f) { _isLerping = true; while(accumulator < 1) { if( _isPaused == false) { accumulator += Time.deltaTime * rate; _lerpVal = Mathf.Lerp(start, finish, accumulator); } yield return null; } _isLerping = false; } public void StopLerp() { accumulator = 1; } public void Pause() { _isPaused = true; } public void Unpause() { _isPaused = false; } } This class could be easily extended to use delegates, handle events, etc, but may be overkill depending on your application.
hey CahMan! again thanks for your input, that sure is handy to have! i just needed to do a small lerp so i ended up doing this: Code (csharp): using UnityEngine; using System.Collections; public class fogcontrol : MonoBehaviour { public float smooth = 2.0f; public float startDistMin=0.0f; public float startDistMax=50.0f; private bool fading; private GlobalFog fogCtrl; private float fade=0.0f; private float timer=0.0f; void Start () { fogCtrl=GetComponent<GlobalFog>(); } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)) fading = true; if(fading) { timer += Time.deltaTime/smooth; fade= Mathf.Lerp(startDistMax, startDistMin, timer); fogCtrl.startDistance=fade; } if(fogCtrl.startDistance <0.1f) { fading=false; timer=0.0f; } print (fade +" "+fading); } }