Setting positions of an object to mouse positions

Hi,

I’ve been trying to set up a rectangle than moves with the X axis of a mouse. Similar to a Breakout paddle. I have the following code:

	public static int zAxisPos	= 0; //Static because other scripts may need to get this.
	private float yAxisPos = -5.23f;
	public float xAxisBoundry = 7.5f;
	public float speed = 10f;

	// Update is called once per frame
	void Update () {
	
		Vector3 mouse = Input.mousePosition;
		Debug.Log (mouse);
		this.transform.position = new Vector3(mouse.x, yAxisPos, zAxisPos);

It works, kind of. The rectangle moves, but the difference in position to the x-axis of the mouse looks fairly big. Is there a reason this would be happening?

The mouse is in Screen coordinates but your objects are in World coordinates. You can convert between the two using Camera.ScreenToWorldPoint(). Note the ‘z’ coordinate is the distance in front of the camera. Search out the many posts on this same issue.

You are amazing @robertbu