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 Navillus · Mar 06, 2014 at 08:11 PM · 2dtransformmouserigidbody2dfollow

Maintain x axis distance between a 2d rigidbody and mouse

Hey guys,

I'm trying to make it so that when the mouse is clicked and held, that the initial distance in the x axis between the 2d rigidbody and the mouse is maintained when the mouse is moved. This 2d rigidbody does not have a collider, so that is not a concern. The y position of the 2d rigidbody is set separately. This is made a little more complicated in that the main camera follows the main character, which is not the 2d rigidbody I've been talking about, so that affects the mouse position.

I created a separate rigid body that follows the mouse, and then I try to compare the transform locations each fixed frame, and move the 2d rigidbody accordingly. This is how I've tried to set the x position of the 2d rigidbody:

desiredObjectLocation.x = lastObjectLocation.x + (mouseLocation.x - lastMouseLocation.x) - (player.position.x - lastCharacterLocation.x);

It's close to working, but I'm missing something. When I move the mouse, the distance between the mouse and the 2d rigid body slowly gets larger or smaller, but I need it to be exact (or closer to).

Can you guys see a small fix to my code, or have a different way of doing this?

Thank you! Andrew

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
0

Answer by wibble82 · Mar 06, 2014 at 08:29 PM

I could be wrong but you might be suffering from a level of feedback due to your calculations changing every frame (it depends where you're setting those 'last x' variables). Floating point numbers are not perfect, so tiny errors can build up over multiple frames if you aren't careful. This often exhibits itself as things slowly drifting away from the correct solution.

How about:

  • when the user first clicks, store the x offset from the mouse to the object

  • every frame, set the desired x position to the new mouse position plus the original x offset

That way, you've calculated the important number up front, and every frame are just updating the new position. As a result, inaccuracies can't build up and you should see more stable behavior.

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 Navillus · Mar 07, 2014 at 12:08 AM 0
Share

Thanks for your comment wibble. I wish I had a reputation high enough to up vote you.

I've been staring at the code for the longest time trying to figure out where there feedback is co$$anonymous$$g from, but I just can't find it. I update the "last x" variables at the end of each fixedupdate frame.

After I couldn't figure out the above, I tried something similar to what you suggested, but it has weird outcomes because the main character is attached to the 2d rigid body by a 2d spring joint.

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

21 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

Related Questions

Dash in the direction of a parents child object 1 Answer

I am making a 2d game and I want to make it where to can pick up bricks with your mouse 3 Answers

Will not rotate when going different speed? 1 Answer

Handling 2D Slope 1 Answer

[Help] Object flips around when using transform.Rotate 3 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