# How to move an object from point of collision to end of the object

Hey there. I am new in Unity and i am having some trouble in figuring out an issue.

I have a sprite which is just a plain square with `BoxCollider2d` on it. A ball is suppose to collide on it, and i want that ball to travel smoothly from point of collision to the end of the square sprite.

I have written some code but it doesn’t seem to be working. The below script is on square sprite on which ball is falling.

``````public class HandleRollerMovement : MonoBehaviour
{

//private Vector3 endPosition = new Vector3(5, -2, 0);
private float desiredDuration = 1f;
private float elapsedTime = 0f;
private Vector3 endPosition;
private Vector3 startPosition;
private Rigidbody2D eggRigidBody;
private BoxCollider2D boxCollider2D;
private float rollerLegth;

private void Start()
{
boxCollider2D = GetComponent<BoxCollider2D>();
rollerLegth = boxCollider2D.bounds.size.x;
}

private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ball"))
{
eggRigidBody = collision.gameObject.GetComponent<Rigidbody2D>();
if (eggRigidBody != null)
{
startPosition = eggRigidBody.transform.position;
Vector2 contactPoint = GetPointOfContact(collision);
float ToMove = Mathf.Abs(rollerLegth - Mathf.Abs(contactPoint.x));

endPosition = new Vector3(startPosition.x + ToMove, eggRigidBody.transform.position.y, 0);
StartCoroutine(MoveEgg());
}
}
}

private IEnumerator MoveEgg()
{
while (elapsedTime < desiredDuration)
{
eggRigidBody.transform.position = Vector3.Lerp(startPosition, endPosition, (elapsedTime / desiredDuration));
elapsedTime += Time.deltaTime;
yield return null;
}
yield return null;
}
Vector2 GetPointOfContact(Collision2D collision)
{
ContactPoint2D contactPoint = collision.contacts[0];
Vector2 contactPosition = contactPoint.point;
return contactPosition;
}

}
``````

There is a small issue with the `elapsedTime` variable not being reset to zero after each collision. This is causing the `MoveEgg` coroutine to only run once because `elapsedTime` keeps increasing after the first collision. To fix this, you should reset `elapsedTime` to zero at the beginning of the `MoveEgg` coroutine.