- Home /
Question by
crashbandit · Nov 13, 2013 at 02:19 AM ·
lerpsmoothtranslate
How do I make this lerp?
I'm trying to make this cube move smoothly one unit at a time, but I can't figure out how to save the position I want it to go to as a variable. My code so far is:
#pragma strict
var newPosition : Vector3;
function Start()
{
newPosition = transform.position;
}
function Update()
{
if(Input.GetKeyDown(KeyCode.UpArrow))
transform.Translate(0,0,1);
if(Input.GetKeyDown(KeyCode.DownArrow))
transform.Translate(0,0,-1);
if(Input.GetKeyDown(KeyCode.LeftArrow))
transform.Translate(-1,0,0);
if(Input.GetKeyDown(KeyCode.RightArrow))
transform.Translate(1,0,0);
}
Can anyone make this move smoothly? Thanks!
Comment
Answer by crashbandit · Nov 16, 2013 at 03:02 AM
I found it myself:
#pragma strict
var newZCoordinate : int;
var newXCoordinate : int;
function Start()
{
newXCoordinate = transform.position.x;
newZCoordinate = transform.position.z;
}
function Update()
{
if(Input.GetKeyDown(KeyCode.UpArrow))
newZCoordinate += 2;
transform.position = Vector3.Lerp(transform.position,Vector3(transform.position.x, transform.position.y, newZCoordinate),Time.fixedDeltaTime * 20);
if(Input.GetKeyDown(KeyCode.DownArrow))
newZCoordinate -= 2;
transform.position = Vector3.Lerp(transform.position,Vector3(transform.position.x, transform.position.y, newZCoordinate),Time.fixedDeltaTime * 20);
if(Input.GetKeyDown(KeyCode.LeftArrow))
newXCoordinate -= 2;
transform.position = Vector3.Lerp(transform.position,Vector3(newXCoordinate, transform.position.y, transform.position.z),Time.fixedDeltaTime * 20);
if(Input.GetKeyDown(KeyCode.RightArrow))
newXCoordinate += 2;
transform.position = Vector3.Lerp(transform.position,Vector3(newXCoordinate, transform.position.y, transform.position.z),Time.fixedDeltaTime * 20);
}
This made the cube move smoothly for me.
Your answer
![](https://koobas.hobune.stream/wayback/20220613121740im_/https://answers.unity.com/themes/thub/images/avi.jpg)