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 portownz · Oct 05, 2011 at 10:45 AM · mouseaxisdragmouse-drag

Drag GameObject with mouse in just one axis

Hello Unity fellows, I have a simple doubt, and already saw all the threads with the 'mouse' tag and cant find the answer. Here is my problem: I need to move one gameobject with the mousedrag operation, and i just want to move it in one specific direction. Some objects ill have to be moved in the Z axis, others in the X. But none in the Y. My game is a 3D Game, and the camera ill look like this: http://i53.tinypic.com/2r76e6o.jpg The camera ill be freezed, the only thing that ill move are these gameobjects. Here is an example of how i want to move the objects: http://i51.tinypic.com/2h7hpj7.jpg

Can anyone help me with tips or even a finished script ? Im making this game for a college project, so i dont have too many time to study programing (im a 3d modeler), but i can try some basic stuff ;) Sry my bad english too! Thanks in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Syksy · Jan 15, 2012 at 12:35 AM

Hello,

Below's a (java) script I used for moving gameObjects in just X-axis direction:

 function OnMouseDrag()
 {
    point = Camera.main.ScreenToWorldPoint(
                Vector3(
                    Input.mousePosition.x,
                    (transform.position.y-Camera.main.transform.position.y),
                    (transform.position.z-Camera.main.transform.position.z)));
 
    point.y = transform.position.y;                   
    point.z = transform.position.z;
    transform.position = point;
 }

Hope this is of assistance. I'm using a 2d orthographic camera and this solution works for my game - I'm not an experienced programmer either, but this approach could be used to also move items in Z-axis direction I believe.

Comment
Add comment · Show 2 · 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 amuro21aya · Jan 26, 2012 at 08:16 AM 0
Share

what if i want to move it up down left and right .. ? thx !

avatar image Syksy · Feb 15, 2012 at 04:29 PM 0
Share

Following the above example and assu$$anonymous$$g you mean you want to drag an object in x- and y-axis directions (while keeping z fixed), the script would be:

function On$$anonymous$$ouseDrag() { point = Camera.main.ScreenToWorldPoint( Vector3( Input.mousePosition.x, Input.mousePosition.y, (transform.position.z-Camera.main.transform.position.z)));

point.z = transform.position.z; transform.position = point; }

If you found the solution helpful please vote :)

avatar image
0

Answer by afeiya · Jul 31, 2012 at 08:39 AM

X and Y worked fine, z didn't work!

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

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

6 People are following this question.

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

Related Questions

MouseLook restrictions not working 1 Answer

How to properly convert mouse pos to world pos 1 Answer

Make the gameobject flip to the direction it is being dragged to? 1 Answer

Drag GameObject only in a specific area 1 Answer

Drag object along Z axis using mouse 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