Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 FabienTT · Aug 04, 2014 at 10:04 AM · translateoffsetmultiple objectsmouse movement

Translating multiple GameObject using the mouse (2D)

Hello !

I've been searching through UnityAnswer, but didn't find the solution to my problem.

My project and camera are set to 2D. My scene is composed of nothing at the beginning, then using the mouse button i can create little GameObject made of a SpriteRenderer and a Collider.

Once a GameObject is created, clicking on it will select it (the gameobject will be added to a List). MouseDragging the gameobject will translate it to the mouse position. When right-clicking the GO get unselected. This is working perfectly. I also implemented a multiple selection function : I am able to create a square in order to select several GameObject. All of them are added to the list.

Then goes the tricky part : When several objects are selected, i would like, when mousedragging one of them, to move all the objects. Of course, If using the same function as for a single selected GO, all the objects will end having the same coordinates, while it's not what am looking for : i want them to keep there relative position. So for each gameobject am computing an offset from the mouse position, that i will add during the dragging to keep the distance. But it is not working : the translation is extremely slow, it's blinking, and when i release the mouse al the GO disappears. Sometime, while dragging, the selection just disappears and error messages are telling me that its position is not valid (infinity).

Here is my code :

 public void MoveSelected(Vector3 mpos){ //mpos are the coordinates of the mouse
 
         Vector3 offset;
 
         foreach (GameObject Selected in CurrSelected) { //CurrSelected is a list where are stored all the Selected gameobjects starting a 2.
                     
                     offset = Selected.transform.position - mpos; //computing the offset between the mouse position and the GO, for each.
 
                     Selected.transform.position += offset; //adding the offset to the new position of the object.
                 }
 
     }


By the way, here is the code am using to determine mpos :

 public Vector3 WorldfromScreen(){
 
         return camera.ScreenToWorldPoint (Input.mousePosition); //Compute world coordinates from screen coordinates
     }


What am i doing wrong ? Thank you very much !

Fabien.

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Prefabs position gets changed 0 Answers

Using gameObject.translate moves it farther than expected 1 Answer

Firing arrow in direction depending on Camera 2 Answers

Plz Help me ! How to tanslate JS to C#? 2 Answers

force traslation 1 Answer


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