- Home /
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;
}
There's no question here.... what are we meant to answer?
Answer by toddisarockstar · Jul 28, 2017 at 10:59 PM
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);
}
This worked out. Was barely a bit off position but I re adjusted accordingly. Thanks!
Answer by fede9948 · Jul 28, 2017 at 10:19 PM
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 ;)
Hey I tried adding this and had a couple problems but figured it out with the other guys reply. Yours was close. But yeah the mirroring was supposed to make it the duplicate go up and down.
Thanks for the help though!
Your answer
![](https://koobas.hobune.stream/wayback/20220612131057im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move an object to mousePosition 2 Answers
How do I go about moving an object side to side using the mouse. 2 Answers
How do I stop an object from cloning when moving? 1 Answer
Mouse position for player movement 0 Answers
How to Touch Drag 3D Objects 2 Answers