Mirroring Object Movement with Mouse Position

Hi I’m working in 2D. The game basically has a paddle at the bottom of the screen going left to right controlled by the mouse, to stop a ball from falling.

I’m trying to have another paddle on the right side of the screen going up and down that is mirroring the paddle at the bottom. This is the script that I have set up for the paddle at the bottom so far, which works fine.

Any help would be great. Thanks!

    private Vector2 paddlePosition;
    private float mousePos;

public void FixedUpdate()
{
	paddlePosition = new Vector2 (0.0f, this.transform.position.y);				

	mousePos = Input.mousePosition.x / Screen.width * 15;

	paddlePosition.x = Mathf.Clamp (mousePos, 1.5f, 13.5f);

	this.transform.position = paddlePosition;

}

put this on your paddle and run it

	public GameObject paddle2;
	private Vector2 paddlePosition;
	private float mousePos;
	public float aspectratio;

	void Start () {

		if(!GameObject.Find("paddle2")){
            paddle2 = Instantiate (gameObject, transform.position, Quaternion.identity)as GameObject;
			paddle2.transform.name="paddle2";
			paddle2.transform.eulerAngles+=new Vector3(0,0,-90);
			paddle2.transform.position=new Vector2(13.5f,0);
		    Destroy(paddle2.GetComponent(this.GetType()));}

	}
	public void Update()
	{   
		aspectratio = (float)Screen.height / (float)Screen.width;
		paddlePosition = new Vector2 (0.0f, this.transform.position.y);
		mousePos = Input.mousePosition.x / Screen.width*15;
	    paddlePosition.x = Mathf.Clamp (mousePos, 1.5f, 13.5f);
        this.transform.position = paddlePosition;
		//remove the apspect ratio multiplication if you dont want to adjust to screen height
        paddle2.transform.position = new Vector2 (paddle2.transform.position.x, (13.5f-paddlePosition.x) * aspectratio);

	}

I’m not sure what you mean by “mirroring” Do you want to move the other paddle up/down or left/right? Or should it be controlled by the mouse up/down movement? However, you could use something like this:

private Vector2 paddlePosition;
private float mousePosX;
//Assign these manually in inspector
public Transform paddle1;
public Transform paddle2;

 public void Update()
 {         
     mousePosX = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
     paddle1.position = new Vector2 (mousePosX, paddle1.position.y);
     paddle2.position = new Vector2 (paddle2.position.x, mousePosX); //Did you mean this by mirroring?
 }

Otherwise, if you want to control one paddle with the mouse left/right, and the other one with the mouse up/down, you could do something like this:

private Vector2 paddlePosition;
private Vector2 mousePos;
public Transform paddle1;
public Transform paddle2;

 public void Update()
 {
     mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     paddle1.position = new Vector2 (mousePos.x, paddle1.position.y);
     paddle2.position = new Vector2 (paddle2.position.x, mousePos.y);
 }

I hope this is what you’re looking for, or at least gives you some inspiration :wink: