- Home /
 
2D game character movement lerping a fixed amount
I am creating a 2D top down game. I need it so that the character moves forwards one unity unit each time the up arrow key is pressed. I have found a way to move the character smoothly, but can currently only control the distance moved by the speed of lerp. Ideally I would also be able to set how long the movement takes, ie a second or half a second.
 var target = 20.0;
 var moveSpeed = 0.5;
 var tParam : float = 0.0;
 
 private var switch : boolean = false;
 
 function Update () {
  if(Input.GetKeyDown("up")){
   MoveForwards();
  } 
 }
 
 function MoveForwards(){
   if (switch) return;
   switch = true;
   var moveTemp : float;
   while (tParam < 0.5){
     tParam += Time.deltaTime * moveSpeed;
     moveTemp = Mathf.Lerp(0, -1, tParam);
     transform.Translate(0, 0, moveTemp);
     yield;
   }
   tParam = 0.0;
   switch = false;
 }
 
               I'm very new to scripting so this is all completely wrong, sorry. Any suggestions are welcome. Hope you can help!
               Comment
              
 
               
              Your answer