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 madslundt · Mar 12, 2013 at 03:31 PM · mousescreentoworldpointworldtoscreenpointmousedrag

Mouse drag gameobject

Hi! I have created a script for dragging my gameobjects with the mouse. The problem is that when the gameobject's z-axis changes the gameobject is not behind the mousecursor. It still moves when the mousecursor changes position, but it is displaced compared to the mousecursor.

How do I make it work so the gameobject always stay behind the mousecursor even when the gameobject's z-axis changes.

My code so far:

 function OnMouseDown() {
         clicked = true;        
         renderer.material.color = Color.blue;
         screenPoint = Camera.mainCamera.WorldToScreenPoint(myTransform.position);
         offset = myTransform.position - Camera.main.ScreenToWorldPoint(
             new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
     }
 
 function OnMouseDrag() {
     var curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
  
     var curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
     myTransform.position = curPosition;
     if (myTransform.position.y > 2.1) {
         myTransform.position.z = 0;
         screenPoint = Camera.mainCamera.WorldToScreenPoint(myTransform.position);
         offset = myTransform.position - Camera.main.ScreenToWorldPoint(
             new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
     } else {
         myTransform.position.z = -4;
         myTransform.localScale.x = 0.5;
         myTransform.localScale.y = 0.5;
         screenPoint = Camera.mainCamera.WorldToScreenPoint(myTransform.position);
         offset = myTransform.position - Camera.main.ScreenToWorldPoint(
             new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
     }
 }
Comment
Add comment · Show 5
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 Meater6 · Mar 12, 2013 at 03:38 PM 0
Share

This may help you: http://answers.unity3d.com/questions/60486/look-at-the-mouse.html

avatar image Catlard · Mar 12, 2013 at 04:59 PM 0
Share

I've had this same problem. It can be solved in several ways. But I'm not sure about some things. First, the z axis means moving away from the camera in your situation, right? Can you show us a video or pictures of this happening?

Your camera also may be set to perspective. This may be the reason that your object appears to not be behind the mouse -- because the camera's view is distorted by distance. Let me know if this was it?

S

avatar image robertbu · Mar 12, 2013 at 06:18 PM 0
Share

Please convert this to a comment. Use the "more" drop down. Answers are reserved for attempted answers to a question. By putting a non-answer here, the question is marked as having an answer and is notably less likely to get an answer.

avatar image madslundt · Mar 12, 2013 at 07:49 PM 0
Share

Yeah sure. I took these pictures of it (the red dot is the mouse cursor)

The first one with z-xis = 0 Z-axis = 0

The second ond with z-axis -4 Z-axis = -4

unity1.jpg (153.0 kB)
unity2.jpg (181.2 kB)
avatar image Meater6 · Mar 13, 2013 at 09:19 AM 0
Share

As robertu mentioned:

Please convert this to a comment. Use the "more" drop down. Answers are reserved for attempted answers to a question. By putting a non-answer here, the question is marked as having an answer and is notably less likely to get an answer.

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

12 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

Related Questions

mouse Input Instantiate 0 Answers

Mouse drag and drop a gameobject javascript 2 Answers

How do I separate OnMouseDown() from OnMouseDrag() 0 Answers

Help positioning an object on a 2D plane 1 Answer

Object instantiates in the same spot (ScreenToWorldPoint) 0 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