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
0
Question by CryFan001 · Dec 29, 2018 at 03:34 AM · rigidbody2daxisaxesrigidbody.movepositionaxe

using rb.MovePosition bugs the y position

I have a ball that has a rigidbody and a circle collider on it. I have the following script applied to it:

 public class PlayerController : MonoBehaviour
 {
     [SerializeField] float speed = 400f;
 
     Rigidbody2D rb;
 
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
     }
 
     void Update()
     {
         float move = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
 
         rb.MovePosition(rb.position + new Vector2(move, 0f));
     }
 }

The ball is in mid air in the scene. When I hit play, the ball falls but it doesn't fall quickly like it did without me writing "rb.MovePosition" method. What's the problem and how can I fix this?

Thanks

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
Best Answer

Answer by sean244 · Dec 29, 2018 at 04:46 AM

Try this

 void FixedUpdate()
 {
     float move = Input.GetAxis("Horizontal") * speed;

     rb.velocity = new Vector2(move, rb.velocity.y);
 }

Since you're using a rigidbody operation, you don't need delta time. Also, you can now lower your speed to something much less, like 20f.

Comment
Add comment · Show 5 · 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 CryFan001 · Dec 29, 2018 at 11:00 AM 0
Share

@sean244 Thanks, it works! But can you explain me why?

avatar image sean244 CryFan001 · Dec 29, 2018 at 11:58 AM 0
Share

So when you’re working with rigidbody, you want to use FixedUpdate rather than Update. This is so that the physics operation will run the same across all devices, regardless of frame rate. As such, you no longer need to multiply the operation by delta time. We multiply certain operations like Transform.translate by delta time because we want them to be frame rate independent. But we’re dealing with a rigidbody operation that is being called in FixedUpdate, so it’s as if it’s already being multiplied by delta time behind the scenes. If you explicitly multiply it by delta time, then it’s like your multiplying it twice by delta time. This causes weird results. That’s why you needed to have such a high speed (400f) for your input to work.

avatar image CryFan001 sean244 · Dec 29, 2018 at 12:13 PM 0
Share

@sean244 I know about the fixed update and stuff but what I don't understand is why you would use rb.velocity over rb.$$anonymous$$ovePosition and why rb.$$anonymous$$ovePosition is not working in my case. Sorry if I sound rude, I do apologize.

Show more comments

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

99 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

Related Questions

Rigidbody2D Normalizing only one axes 0 Answers

xsi to fbx model has inverted axis 0 Answers

Creating an axes representation on the screen? 0 Answers

moveposition() not working when slowing down the game!,rigidbody.moveposition not working when slowing down the game 0 Answers

Inhibit force from doubling when 2 axes are active 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