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 Dubmulik · Sep 01, 2020 at 09:03 PM · rigidbody2dmousepositionmouse-dragdelta position

Moving Rigidbody2D relative to the mouse.

I made this script to slide a ball with a mouse:

 public class Movable : MonoBehaviour { 
     private Rigidbody2D rb2d;
     private Vector2 deltaPos  = Vector2.zero;
     private Vector2 currentPosition;
     private Vector2 lastPositon;
     
     void Start() {
         rb2d = GetComponent<Rigidbody2D>();
     }
 
     void Update() {
         
         if (Input.GetMouseButtonDown(0)) {
             lastPositon = Input.mousePosition;
         }
 
         if (Input.GetMouseButton(0)) {
             currentPosition = Input.mousePosition;
             deltaPos = currentPosition-lastPositon;
             lastPositon = currentPosition;
         } else {
             deltaPos = Vector2.zero;
         }
         
         if (Input.GetMouseButtonUp(0)) {
             deltaPos = Vector2.zero;
         }
     }
 
     void FixedUpdate()
     {
         rb2d.MovePosition(rb2d.position + deltaPos *Time.fixedDeltaTime );
     }
 }



The issue here is that the ball is not 100% in sync with the mouse. Here is a gif to demonstrate: https://imgur.com/yQLvODH

  • A) Code used from this video: https://www.youtube.com/watch?v=eSdjNGNj6uk

  • B) Script I posted above. Compared to option A where the ball tries to go to the target position and just jumps once obstacle is not in the way in this option ball is moved with delta and will just stop moving when it cant.

  • C) Just to highlight the issue with my own approach. The ball should follow the mouse 1:1 as long as there are no obstacles. It is ok for the ball to stay behind when there is an obstacle, but once the obstacle is gone it should start following the mouse again.

    Is the delta calculation wrong or what is causing the ball to not follow the mouse precisely?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by GrayLightGames · Sep 01, 2020 at 09:55 PM

Hi @Dubmulik, my guess is that the problem has to do with the fact that Update and FixedUpdate don't execute in sync. For example, your Update function might run twice before FixedUpdate runs, so if your mouse moves 1 pixel each Update and it runs twice, deltapos is only 1 pixel and the other pixel is not processed by the FixedUpdate code. Have you tried just putting all of the logic into Update? That's what the Youtube video shows. If you really want to leave the MovePosition in FixedUpdate, you could create a value to store all the movement in Update, add to it in Update and then clear it in FixedUpdate when it's processed. Something like what you have plus the following: private Vector2 movementSinceLastFU = Vector2.zero; void Update(){ movementSinceLastFU += deltaPos; } void FixedUpdate(){ rb2d.MovePosition(rb2d.position + movementSinceLastFU); movementSinceLastFU = Vector2.zero; } So yeah, I would first try just putting that MovePosition (without deltaTime) into Update to see if that is what is causing your issue. Then you can decide if leaving it there is OK or if you want to try the additive approach. Hope that helps!

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 Dubmulik · Sep 02, 2020 at 05:07 PM 0
Share

That was the reason! So obvious! Thanks for pointing it out.

avatar image GrayLightGames Dubmulik · Sep 02, 2020 at 05:28 PM 0
Share

Cheers, good luck with your project!

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

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

Related Questions

Move mechanical arm with mouse 0 Answers

How to I mouse Drag sprite diagonally only? 0 Answers

Rotate Object with mouse in RTS style game 2 Answers

Orbital cannon jitters while following/facing mouse position 1 Answer

Issue with mouse position script 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