Problem with clic on a button then Mathf.Lerp a float during Time.time.

Hi,

I still don’t get it with the time in Unity.

what i want to do:

if button pressed → lerp a float (from 400 to 200) in 3 secondes.

if I’m using a Time.deltaTime I’ve got a flicking.

if I’m using Time.time and set it up with a Time.time*0.5 for example, my movement is right at the start of the game and instantaly after few seconds.
I know that this comes from the fact that Time.time is the start in seconds since the start of the game.
The Time.deltaTime comes use the frame rate (am I right ?) so it still not accurate.

How could I do to have that right ?

    void Update()
    {
        if (Slide == true)
        {
            StartCoroutine(SlidingPanel());
        }    
    }      

    void OnGUI()
    {
        //-------------------------------------------------Tests
        if (GUI.Button(new Rect(50, 50, 50, 50), "", Close))
        {
            Slide = true;
        }
        GUI.Label(new Rect(SPW, 0, 100, SH),"",Close);
    }

    IEnumerator SlidingPanel()
    {
        SPW = Mathf.Lerp(SW, SW - 100, Time.time);
        yield return 0;
    }

The theory of time in Unity is that it moves forward 1 for every second of game time. So if you want an object to move at specific times using time by it’s self is unwise. The key would be to store the time of the event, then subtract that from your current time. That gives you a number that you can measure against.

So Time.time-startTime = timeSinceEvent. So say it is 1.0 seconds after the event and your duration is 3 seconds. So… 1.0/3.0 would be the number of your lerp (roughly 0.333) Lerp specifically uses a number clamped between 0 and 1. So anything greater than 1 is just 1. So 0.333 is perfect.

This coupled with another thread you posted is probably what you are asking for in general:

// move distance over duration
var distance=Vector3(5,0,0);
var duration=3.0;

private var moving=false;
private var direction=false;
private var amount=0.0;
private var startTime = 0.0;
private var startPosition : Vector3;

function Start(){
	// store the start position for use in the script
	startPosition=transform.position;
}

function Update(){
	if(moving){
		// lerp according to the direction.
		if(direction){
			amount=Mathf.Lerp(1,0,(Time.time - startTime) / duration);
		}else{
			amount=Mathf.Lerp(0,1,(Time.time - startTime) / duration);
		}
		
		// set the transform.position to the start position + the distance * the current amount
		transform.position=startPosition + distance * amount;
		
		// check to see if we have reached our direction goal
		var hasEnded=(transform.position == startPosition  direction) || (transform.position == (startPosition + distance)  !direction);
		if(hasEnded){
			// if so, stop moving and change the direction to the opposite way
			moving=false;
			direction=!direction;
		}
	}
}

// when we click on the object, start the moving process
function OnMouseDown () {
	if(!moving){
		// make sure to store the time we clicked so that we can lerp from that point forward.
		startTime=Time.time;
		moving=true;
	}
}

Thank you very much for your help, it’s very much apprieciated :smile:

I’m going to try that.

edit: Works fine Cheers !