# Move Object from variable point A to Variable point B

Hey I am making a mobile game and have a problem moving my player object.

My game recognizes the input and teleports the player object to the input coordinates, but I want my object to move to the position and not teleport. I have added a `Move Towards` function ( see script) and now it looks like it wants to move to the position but doesn’t move along the y-axis.

### Does anybody know how to fix this?

P.S.: I am fairly new to coding and Im still trying to learn and understand

``````public Rigidbody2D rb;
public GameObject Player;
public GameObject PauseBackground;
private bool canInput = true;
public float movementSpeed = 10f;

void Start()
{
PauseBackground.SetActive(false);
}

void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Wall"))
{
transform.position = collision.contacts[0].point;
rb.constraints = RigidbodyConstraints2D.FreezeAll;
}

if (collision.gameObject.CompareTag("Death"))
{
Destroy(Player);
}

if (collision.gameObject.CompareTag("Finish"))
{
}
}

void Update()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began && canInput == true)
{
float step = movementSpeed * Time.deltaTime;
Vector2 touchPosition = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);

// Ensure the y-coordinate matches the GameObject's y-coordinate
touchPosition.y = transform.position.y;
transform.position = Vector2.MoveTowards(transform.position, touchPosition, step);

// Check if input is received
Debug.Log("Touch position" + touchPosition);

// Unlock position constraints
rb.constraints = RigidbodyConstraints2D.None;

// Disables the input for 1 second
DisableInput();
}
}

IEnumerator DisableInputForSeconds(float duration)
{
canInput = false;
yield return new WaitForSeconds(1f);
canInput = true;
}

public void DisableInput()
{
StartCoroutine(DisableInputForSeconds(1f));
}

{
PauseBackground.SetActive(true);
}
``````

Oh, I missed this detail in your code

``````// Ensure the y-coordinate matches the GameObject's y-coordinate
touchPosition.y = transform.position.y;
``````

You are explicitly overriding the y-position of the touch to match the current y position of the object. This means the `MoveTowards` will never try to change the y value.

Does your `RigidBody2D` have gravity enabled? If so, then maybe gravity is preventing movement in the y direction.

It is an I also tried it with no gravity but didn’t seem to work either

I had deleted this function as well but the behavior didn’t change.
I fixed the issue turned out that the problem was that the MoveTowards function didn’t work properly because it was set in this if function. I created a bool and made a different if statement.
My object now moves to the touched point just have to solve the issue with the `Rigidbody2D`.
When touching onto an empty space I want it to have no constraints, but freeze all constraints when touching something (see `OnColllisionEnter2D`). When a point is touched and the rb has no constraints it falls to the ground and gets its constraints but doesn’t freeze in position, but moves to the touched coordinates.

``````void Update()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began && canInput == true)
{
touchPosition =(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position));
moving = true;

// Unlock position constraints
rb.constraints = RigidbodyConstraints2D.FreezeAll;

// Disables the input for 1 second
DisableInput();
}

if(moving && (Vector2)transform.position != touchPosition)
{
float step = movementSpeed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, touchPosition, step);
rb.constraints = RigidbodyConstraints2D.None;
}
else
{
moving = false;
}
}
``````

There Are Many Functions : `Move Towards`, `Translate` & `transform.position`.