Why my Player GameObject keeps on mooving anyway on its own without my input?

Hi All,

Sorry for my bad english!

I’m very beginner in unity
I have a minigame 2D made of just 3 Levels, in which i have My Player, that is a simple Game Object with a animated sprite, and at some moments of the game, i istantiate some animated prefabs that i connect to the Player, making the prefabs children of the player.
Then, i can drag the Player it by mouse(PC) or by touch(Android/Iphone) insiede a very simple Maze o few streets. I use the rigid body connected to the player.
I program everything by C#.
My problem is this:
At the Level 1 everything works good, it means that my player mooves when i put a dragging input by mouse or touch and stop to mooves when i stop to drag or touch.
At the level 2 and 3, instead, when i stop draging or touching the player, the players keeps on mooving on it’s own as if someone else was touching it.
How is my player able to moove its own without my input?
I don’t know how to fix this bug.
Please help me.
Thank you very much for all the future help

The next is a part of the script attached to my Player. It’s in C#

void Update()
         {
            if (Input.touchCount > 0) 
	{
		TouchPhase touchphase= Input.GetTouch(0).phase;

		switch (touchphase)
		{
		case TouchPhase.Began:
                   animator.SetInteger("a", 1); //mooving
			break;
	
		case TouchPhase.Stationary:
			animator.SetInteger("a", 0); //idle
			break;
		case TouchPhase.Moved:	
                    animator.SetInteger("a", 1); //mooving
                     float x = Input.GetAxisRaw("Mouse X");
			float y = Input.GetAxisRaw("Mouse Y");
		Vector2 pos = new Vector2 (Input.GetTouch (0).position.x, Input.GetTouch (0).position.y);
			pos = Camera.main.ScreenToWorldPoint (pos);	
               //in the Start() function, rb2d = this.gameObject.GetComponent<Rigidbody2D> ();
			rb2d.MovePosition (pos); 
				break;

		case TouchPhase.Ended:
			animator.SetInteger ("a", 0); //idle
				break;
		}

}

//-------------------------------------------------------------------------------------------

void OnMouseDrag()
	{

		float x = Input.GetAxisRaw ("Mouse X");
		float y = Input.GetAxisRaw ("Mouse Y");
		
		Vector2 pos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
		
		pos = Camera.main.ScreenToWorldPoint (pos);	

		rb2d.MovePosition (pos);
		
	}

Because if you center your camera to player object and your character moves, your code: pos = Camera.main.ScreenToWorldPoint (pos); will always return a different value, because after your character moved, the camera moves, so the screen point starts to refer a different point on world space. This is like carrot on a stick.

To overcome the issue, save your world poisiton to a vector3 and use it for things instead of casting screen position every time. Adn only update it if required (in your case, it is touch move, i guess).