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
1
Question by SBull · Feb 08, 2017 at 08:24 PM · mousemousepositionmouse-drag

How would I make the mouse drag move smoothly?

I have a 2D game with a slingshot mechanic, where the player clicks an object and drags it to aim where it will launch. However, it does not seem to be very smooth. If I move the mouse position very slightly it doesn't change the aiming position until the mouse is a certain distance, then it will "snap" to the next x or y coordinate. I have tried lerping this, but that only gives the appearance of more precise aiming, because it smoothly transitions between the snapped positions, but does not actually allow for the mouse to settle in between snap points. So how would I make the mouse position be more precise? Here is the code I am using for the dragging function.

 function OnMouseDrag()
 {
         var pos = Input.mousePosition;
         pos.z = -Camera.main.transform.position.z;
         pos = Camera.main.ScreenToWorldPoint(pos);
         
         //Clamps launching object to a circle around the launch point
         var newPos = pos + offset;
         transform.position = startPos + Vector3.ClampMagnitude((newPos - startPos), radius);
         
         //Clamps launching object from moving too far up or forward
         transform.position.x = Mathf.Clamp(transform.position.x, -100, launchPivot.transform.position.x);
         transform.position.y = Mathf.Clamp(transform.position.y, -100, launchPivot.transform.position.y);        
     }
 }
Comment
Add comment · Show 3
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 SBull · Feb 09, 2017 at 04:24 AM 0
Share

I made a discovery after some experimentation and figured out what the issue is, but I am still unsure how to solve it. I did a Debug.Log(pos.ToString("F3")); to see how sensitive the positions were and it would give me results showing that changes were occurring out to several decimal points. For example (1.234, 1.234, 1.234). But when I tried Debug.Log(Input.mousePosition.ToString("F3")); it showed only whole numbers. For example (1.000, 1.000, 1.000).

This may just be my ignorance on how Input.mousePosition works, but could this have something to do with the imprecise mouse position?

avatar image MESSI007 · Mar 16, 2017 at 04:08 PM 0
Share

thank you , you saved my day by mentioning the problem cause ;

avatar image MESSI007 · Mar 16, 2017 at 04:38 PM 0
Share

i got a solution to get mouse position with more precision :D

   void On$$anonymous$$ouseDown()
 
     {mousePosition = Input.mousePosition;
     }
 void  On$$anonymous$$ouseDrag()
 {
  mousePosition.x += Input.GetAxis("$$anonymous$$ouse X")*5;
             mousePosition.y += Input.GetAxis("$$anonymous$$ouse Y")*5;
             print(mousePosition.ToString("F5"));
             v2 = Camera.main.ScreenToWorldPoint(mousePosition);
 }




1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · Feb 08, 2017 at 08:29 PM

OnMouseDrag, in my experience, is more for drag and drop. You'd probably be much better off running the code in your update function and track the mouse position/state manually. That way it would match the mouse position every frame regardless.

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 SBull · Feb 09, 2017 at 03:28 AM 0
Share

I thought that sounded like it might fix the issue, but I tried putting the function in Update and unfortunately the problem didn't change. It still snaps to certain positions.

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

66 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How to prevent GameObject from spawning on top of each other? 1 Answer

Why is OnDrag triggering but not OnMouseDown? 0 Answers

3D mouse position 0 Answers

Ray Over Mouse 1 Answer

How I can save mouse position ? 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