Move smoothly from left to right side of screen

I feel like I’m missing something here. I’m currently trying to lerp on touch from current position to my destination. For whatever reason it just does not want to work. I’m trying to replicate the movement of Break Liner by Ketchapp.

Break Liner (Ketchapp) - YouTube

Another idea I had was to use a rigidbody2d and have the player moving up and then lerping to the position but that does not work either. Posting code below.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class InputManager : MonoBehaviour
{
    public GameObject player;
    public float speed = 1f;
    // Use this for initialization
    void Start ()
    {
        player.transform.position = new Vector3(-2, 0, 1);
    }
   
    // Update is called once per frame
    void Update ()
    {
        PlayerInput();
        player.GetComponent<Rigidbody2D>().velocity = Vector2.up;
    }
 
    public void PlayerInput()
    {
        if(Input.GetMouseButtonDown(0))
        {
            if(player.transform.position.x == -2)
            {
                Debug.Log("Pressing");
                player.transform.position = Vector3.Lerp(transform.position, new Vector3(2, 0, 1), 1);
            }
            else if(player.transform.position.x == 2)
            {
                Debug.Log("Pressing Two");
                player.transform.position = Vector3.Lerp(transform.position, new Vector3(-2, 0, 1), 1);
            }
        }
    }
}

To add a little bit more detail: when I change to

player.transform.position = Vector3.Lerp(transform.position, new Vector3(2, 0, 1), Time.deltaTime);

instead of lerping to the new value of 2 on the X, the players nnew position is -43x 20y -4z. What is causing the plays position to be so far off from the new vector I’m giving it?

have you checked out the docs about vector3.lerp?
could be usefull for your problem.


EDIT: I took out the Example of the Vector3.Lerp from UnityDocs, added fixed Code of your Question.
Hope it’s what you’re looking for. changed some lines inside Of the Transform (Y coordinate is always the transform.position.y coordinate or you will stay always grounded and never go up(driven from the Riggidbody2D)), because the Input.MouseButton is a OneTime event and for Lerping you need a Update Function, which you can see below. Further more I added a bool which functions like a PingPong effect and added Speed to your Lerping function so it’s a Smooth transition.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
  
public class InputManager : MonoBehaviour
{
	 public float posOne = 2,posTwo = -2;
     public GameObject player;
     public float speed = 1f;
	 public bool m_switch;
	 
     // Use this for initialization
     void Start ()
     {
         player.transform.position = new Vector3(posTwo, 0, 1);
     }
    
     // Update is called once per frame
     void Update ()
     {
         PlayerInput();
         player.GetComponent<Rigidbody2D>().velocity = Vector2.up;
     }
  
     public void PlayerInput()
     {
		 
         if(Input.GetMouseButtonDown(0))
         {
			m_switch = !m_switch;
         }
		  	if(m_switch){
                Debug.Log("Pressing");
                player.transform.localPosition = Vector3.Lerp(transform.position, new Vector3(posOne, transform.position.y, 1), Time.deltaTime * speed);
			}
            else if(!m_switch){
                Debug.Log("Pressing Two");
                player.transform.localPosition = Vector3.Lerp(transform.position, new Vector3(posTwo, transform.position.y, 1), Time.deltaTime * speed);
			}

	}
}