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
1
Question by didibus · May 17, 2010 at 06:17 PM · transformmousetranslate

Have a gameObject follow the mouse in sync. So that the mouse pointer is always over the object.

Hi,

I have a very simple scene that contains a cube. I want to be able to drag the cube around with the mouse, in a way that syncs perfectly with the mouse. The mouse cursor should always remain over the object, and visually, the object should move as much as the mouse did.

Here's the code I have now:

xx = Input.mousePosition.x; yy = Input.mousePosition.y;

         Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(xx, yy, cube.transform.position.z));

         cube.transform.Translate((pos.x - camera.transform.position.x), (pos.y - camera.transform.position.y), 0);

This works fine when I put the camera in Orthographic mode. So I'm guesing the problem is the perspective isn't taken into account. I should have my cube move more when it is far, and move less when it is close, so that its always in sync with the mouse, but I don't know how to do it.

Thank you.

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
0
Best Answer

Answer by Ashkan_gc · May 17, 2010 at 06:28 PM

the third argument of ScreenToWorldPoint is the distance between camera and the cube. also in last line you can set the

cube.transform.position = pos;

i have a code that i am sure that it works

while (Input.GetMouseButton(0))
{
temp = Input.mousePosition;
temp.z = cube.transform.position.z- mainCamera.transform.position.z;
 transform.position = mainCamera.ScreenToWorldPoint(temp);
}
Comment
Add comment · Show 4 · 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 didibus · May 17, 2010 at 07:01 PM 0
Share

This made the trick, thanx.

temp.z = cube.transform.position.z- mainCamera.transform.position.z;

avatar image Ashkan_gc · May 18, 2010 at 05:03 AM 0
Share

yes it's the trick. as the documentation says z argument should be the distance from the camera but i don't know why people don't read it well. i myself had problem realizing it for some hours. i set this to 0 and my code did not work. i read the docs again and said oops :)

avatar image Babilinski · Nov 05, 2011 at 02:10 PM 0
Share

is there a way to make the mouse follow the object ?

avatar image tonydincau · Oct 28, 2012 at 11:34 PM 0
Share

Thanks very much for this! :)

avatar image
-3

Answer by dashmasterful · Mar 21, 2013 at 10:26 AM

this does not work

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 sdgd · Mar 21, 2013 at 10:38 AM 0
Share

please make an new question or comment and not an answer

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Mouse Pointer Transform.Position? 1 Answer

Vector3 Returning Infinity Error 2 Answers

Transform.Translate change distance and speed 1 Answer

Wait a fraction of a second within a Function Update() 1 Answer

How To Drag any Cube Using Mouse Click and Moved Only 1 Unit?? 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