Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ILoveMyDada · Jul 24, 2017 at 10:37 PM · movementobjectmousepositionmirror

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;

 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FortisVenaliter · Jul 28, 2017 at 08:57 PM 0
Share

There's no question here.... what are we meant to answer?

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best 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);
 
     }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ILoveMyDada · Aug 01, 2017 at 06:43 AM 0
Share

This worked out. Was barely a bit off position but I re adjusted accordingly. Thanks!

avatar image
0

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 ;)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ILoveMyDada · Aug 01, 2017 at 06:42 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges