I am making a 2.5d game with three “paths” lined up along the z axis, the player is moving forward along the x axis, i am trying to make it so that when you swipe up on the screen it moves “25.0f” on the z axis and when you swipe down it does the same downwards. This is the code i am using:
public float moveCar = 25.0f;
//First establish some variables
private Vector3 fp; //First finger position
private Vector3 lp; //Last finger position
public float dragDistance; //Distance needed for a swipe to register
// Update is called once per frame
void Update()
{
//Examine the touch inputs
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fp = touch.position;
lp = touch.position;
}
if (touch.phase == TouchPhase.Moved)
{
lp = touch.position;
}
if (touch.phase == TouchPhase.Ended)
{
//First check if it's actually a drag
if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
{ //It's a drag
//Now check what direction the drag was
//First check which axis
if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
{ //If the horizontal movement is greater than the vertical movement...
if (lp.x>fp.x) //If the movement was to the right
{ //Right move
//MOVE RIGHT CODE HERE
}
else
{ //Left move
//MOVE LEFT CODE HERE
}
}
else
{ //the vertical movement is greater than the horizontal movement
if (lp.y>fp.y) //If the movement was up
{ //Up move
transform.Translate (Vector3.right * moveCar);
}
else
{ //Down move
transform.Translate (Vector3.left * moveCar);
}
}
}
else
{ //It's a tap
//TAP CODE HERE
transform.Translate (Vector3.up * 6);
}
This does what i want, except that the movement isn’t smooth at all, it just kind of teleports to the new location, i was wondering how i could make it move to the new position smoother. I have tried looking into the Vector3.Lerp but didnt really understand how to implement it.
Any tips on how i could achieve the smoother movement effect?