Modifying scroll texture

Hi.

How can I modify the scroll script so that it scrolls the texture everytime a button is pressed for one second for example and then stops? The way I tried it only plays it on some frames doesn’t play the whole scrolling like it is without the Input.GetKeyDown. Basically, I want to be able to play with it, how much time it will scroll, then stop? Thanks.

// Scroll main texture based on time
var scrollSpeed : float = 0.5;

function Update() {
	
	if(Input.GetKeyDown ("5"))
	{
	
    var offset : float = Time.time * scrollSpeed;
    renderer.material.mainTextureOffset = Vector2 (offset, 0);
}
}

It can be done with some modification of your original script. I forced the offset value to be modulo 1 in order to avoid a well known bug with huge offsets:

// Scroll main texture based on time
var scrollSpeed : float = 0.5;
var scrollTime : float = 1; // scroll duration in seconds
private var endScroll: float = 0;
private var offset: float = 0; // offset now is permanent instead of temporary

function Update() {

    if(Input.GetKeyDown ("5"))
    {  // set end scrolling time
        endScroll = Time.time + scrollTime;
    }
    if (Time.time < endScroll)
    {
        offset += Time.deltaTime * scrollSpeed; 
        // use offset modulo 1 to avoid a known problem with big offsets
        renderer.material.mainTextureOffset = Vector2 (offset % 1, 0);
    }
}

EDITED: Ok, I got it: the scroll started at random positions because it was based on Time.time. I edited the script to scroll after the last position: the offset variable was moved out of the function to become permanent, not temporary, so it can “remember” the previous position.