Input.mouseposition

using UnityEngine;

using System.Collections;

public class Basket : MonoBehaviour

{

void Update ()
{
	// Get the current screen position of the mouse from Input
	Vector3 mousePos2D = Input.mousePosition;

	// The Camera's z position set the how far to push the mouse into 3D
	mousePos2D.z = -Camera.main.transform.position.z;

	// Convert the point from 2D screen space into 3D game world space
	Vector3 mousePos3D = Camera.main.ScreenToWorldPoint (mousePos2D);

	// Move the x position of this Basket to the x position of the Mouse
	Vector3 pos = this.transform.position;
	pos.x = mousePos3D.x;
	this.transform.position = pos;
}

}
I am currently learning c# from a book. This is the one of the code inside. Can someone explain the content in void update? The book didnt explain it clearly to newbies like me, so plz help me. Thank you

Unity has multiple coordinate systems. The mouse uses 2D screen coordinates that start at (0,0) in the lower left corner and go to (Screen.width, Screen.height) in the upper right. Objects live in world coordinates which is a 3D space. So image you are standing at the window and touch the glass with your finger. To figure out a world space beyond the window, you would need to know at what distance in front of the window.

That is how this code works. Line 4 get the position on the ‘glass’. Line 7 says how far in front of the window to find the point (assumes your objects are at z = 0 and the camera is looking towards positive ‘z’). Line 10 convert to the world position.

Apparently this code wants objects to only follow on the ‘x’ axis of the mouse, so that is how lines 13 - 15 do their job.