move object style snake

how can I make sure to tell my object when I press the right arrow he must go infinitely right? and when I click the left must do the opposite? I have tried using if (Input.GetAxis) but right when I remove your finger from the button stops this is the script:

#pragma strict

function Start () {



}

function Update () {

  if(Input.GetAxis("Vertical")>0){
  
  transform.Translate(0,0,5.0*Time.deltaTime);
  }
  
  }

Not sure but couldn’t you use GetButtonUp in place of GetAxis? that was when you unpress the button the snake moves and changes when another direction is pressed and released?

I want to do is that when the button is released, the object must continue to follow the same direction until the next input

You’ll want to do things a little differently. If you always want to move, the translate needs to be in the Update without being in an if statement. You’ll need to store the direction the user last chose so when the input stops your script knows which way it’s going in the subsequent updates.

var direction : Vector3 = Vector3.up;
var speed : float;

function Update()
{
	transform.Translate(direction * speed * Time.deltaTime); // always move

	if(Input.GetAxis("Vertical") <> 0) // change direction based on input
	{
		if(Input.GetAxis("Vertical")>0)
		{
			direction = Vector3.up;
		}
		else
		{
			direction = Vector3.down;
		}
	}
}

(not tested code, but you get the idea)

You could do something similar by always using Vector3.forward, and then rotate the object based on input

But “Vector 3” and “transform.Translate” I can not assign to any variable? I tried so but it does not work

var a : Vector3;

a = transform. Translate (0,0,5*Time.deltaTime);