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 Mosy · Aug 27, 2014 at 05:42 PM · draganddrop

OnMouseDrag() Problems C# script

Ok so I am trying to make a game with a perspective camera that is at an angle to the game play world. In this game there are objects which need to be selected by the player and then dragged into position to act as barriers. I did a quick search and found a similar question and tried to implement the code below.

 void OnMouseEnter()
 {
     if(Input.GetMouseButtonDown(0)){
         screenPoint = Camera.main.WorldToScreenPoint(scanPos);
  
         offset = scanPos - Camera.main.ScreenToWorldPoint(
             new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
         isMoving = true;
     }
     if(Input.GetMouseButtonUp(0)){
         isMoving = false;
     }
 }
  
 void OnMouseDrag()
 {
     Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
  
     Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
     transform.position = curPosition;
 }

I had to modify it, because in my game world Y is up, so that the object wouldn't try to get some love from the camera. So I fixed the Y to keep the object on the ground which was working perfectly, but the object would still move to the camera's position and just stay on the ground, unless it felt like moving to some other random place, because it did that at times too. However here is the thing that really made me go WTF? If I selected the object in the scene view or the heirarcy and then tried to click and drag it in the game view it would move up to the fixed Y position in the spawn location, and not move again.

Then I had an idea to try and move the object by just taking in the difference of the mouse position and adding that to the transform.position of the object, and so I modified the code to look like this.

 void OnMouseEnter()
     {
         currMousePos = new Vector3(Input.mousePosition.x, 1, Input.mousePosition.y);
         lastMousePos = currMousePos;
         if(Input.GetMouseButtonDown(0))
         {
             isMoving = true;
         }
     }
 
     void OnMouseDrag()
     {
         while (isMoving)
         {
             currMousePos = new Vector3(Input.mousePosition.x, 1, Input.mousePosition.y);
             Vector3 curPosition = currMousePos - lastMousePos;
             transform.position += curPosition;
             lastMousePos = currMousePos;
             if (Input.GetMouseButtonUp(0))
             {
                 isMoving = false;
             }
         }
     }

Which does absolutely nothing, because now the object just sits there at the spawn location. Please help me.

Comment
Add comment · Show 2
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 robertbu · Aug 28, 2014 at 05:36 AM 1
Share

It is hard to give an answer when you don't include the whole script. So I'm left guessing about the value of 'screenPoint' and 'scanPos'.

but the object would still move to the camera's position and just stay on the ground

When you do a Camera.ScreenToWorldPoint() with a perspective camera, the 'z' passes should be the distance in front of the camera. If 0.0 is passed for the 'z' value, then ScreenToWorldPoint() will always return the position of the camera regardless of where the cursor is on the screen. And this sounds like the behavior you re getting, so I'm guessing that 'screenPoint.z' is at or near 0.0. Replace the value with some positive distance in front of the camera.

Note, you don't give your setup, but if the camera is at an angle with respect to the plane the characters are walking on, you have to change to a very different approach to solve this problem.

avatar image Mosy · Aug 28, 2014 at 07:12 PM 0
Share

Other than Update() which is only telling the object to rotate to face the correct direction, count down the timer, and when the timer reaches 0 destroy the object that is the entirety of the script.

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

snap to point remove 0 Answers

How to drag Unity Objects to other c# application 0 Answers

DragAndDrop class bugged? 1 Answer

Drag 2dtexture from inventory to game. 0 Answers

Dragrigidbody help 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