Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question RigidBody2D Causing Character to Move Slow?

Discussion in '2D' started by Storm1208, May 14, 2020.

  1. Storm1208

    Storm1208

    Joined:
    Nov 10, 2019
    Posts:
    5
    Hey all, first time poster so I hope I'm doing this right. I'm following the "Ruby's Adventure" tutorial to try and get my feet wet with the Unity Engine workflow. Everything was going good until I got to the "World Interactions" section of the tutorial.

    During this part, the tutorial had me add a collider to the main character and then replace my "transform" based movement system with a rigidbody2D based one. Essentially the code went from this:

    Code (CSharp):
    1. public class RubyController: MonoBehaviour {    
    2. // Start is called before the first frame update  
    3. void Start()     {              
    4.  
    5. }    
    6.  
    7. // Update is called once per frame    
    8. void Update()     {        
    9. float horizontal = Input.GetAxis("Horizontal");        
    10. float vertical = Input.GetAxis("Vertical");                
    11. Vector2 position = transform.position;        
    12. position.x = position.x + 3.0f* horizontal * Time.deltaTime;         position.y = position.y + 3.0f * vertical * Time.deltaTime;        
    13. transform.position = position;    
    14. }
    15. }
    To this:

    Code (CSharp):
    1. public class RubyController : MonoBehaviour {  
    2. Rigidbody2D rigidbody2d;        
    3.  
    4. // Start is called before the first frame update  
    5. void Start()     {        
    6. rigidbody2d = GetComponent<Rigidbody2D>();    
    7. }  
    8.  
    9. // Update is called once per frame    
    10. void Update()     {        
    11. float horizontal = Input.GetAxis("Horizontal");      
    12. float vertical = Input.GetAxis("Vertical");                
    13. Vector2 position = rigidbody2d.position;        
    14. position.x = position.x + 3.0f* horizontal * Time.deltaTime;         position.y = position.y + 3.0f * vertical * Time.deltaTime;        
    15. rigidbody2d.MovePosition(position);    
    16. }
    17. }
    I followed all the instructions to a "T" but when I finished implementing the code I noticed my character moved significantly slower. I've done some digging on the Unity forums and other parts of the internet but I couldn't find anything similar to my issue. If anyone knows what might be causing it I could really use some help!
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    The issue is rigidBody2D.MovePosition(). Because physics is updated with FixedUpdate and MovePosition only takes the last position it received. It will appear to slow down because its not receiving all the positions you are sending it. Why? FixedUpdate or what I call the physics update is called every 0.02s(50fps), on average Update is called every 0.016s(60fps). With physics "lagging behind" it moves slower using that method. I usually set the velocity directly or use AddForce to move a rigidBody using Update.
     
    SlimeNull and Storm1208 like this.
  3. Storm1208

    Storm1208

    Joined:
    Nov 10, 2019
    Posts:
    5
    Thanks for the clarification! I ended up remedying my issue after reading what you said and watching a video about top down movement from Brackeys that detailed the difference between update and fixed update. I implemented a system where the player's input is taken in the regular update function, but all of the rigid body based movement happens in fixed update. It's working perfectly now!

    I find it kind of crazy that an error like that was in an official Unity tutorial though. I can't have been the first person to run into that issue while following it. Did Unity handle updating differently in a previous version maybe?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    I find it odd you state that when the official code executes the move position like this which is clearly during fixed update:
    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         Vector2 position = rigidbody2d.position;
    4.        
    5.         position = position + currentInput * speed * Time.deltaTime;
    6.        
    7.         rigidbody2d.MovePosition(position);
    8.     }
     
  5. Storm1208

    Storm1208

    Joined:
    Nov 10, 2019
    Posts:
    5
    Are we looking at the same tutorial? I took a screenshot of the instructions for Ruby's Adventure and they clearly put the rigidBody2D movement code in Update, not FixedUpdate:

    rubyproof.JPG

    This is an actual screencap from the tutorial itself.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    Storm1208 likes this.
  7. Storm1208

    Storm1208

    Joined:
    Nov 10, 2019
    Posts:
    5
    That certainly is strange. I was following the version from the Unity Learn website so perhaps that one is outdated? Thank you for highlighting that it's correct in the final version though!
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    Right, I took a look at it and it seems that the step-by-step part has not been updated. I'll send a message to the Learn team about this.

    Thanks!
     
    Cul0man and Storm1208 like this.
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    Just an update to say that I spoke with the Learn content team today and they have a document with the changes made to the main project and have confirmed that they will update the step-by-step instructions to match.
     
    Chris-Trueman and Storm1208 like this.
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    I was just told that the changes have now gone live!

    Thanks for reporting and thanks to the content team for a fast turn around. :)
     
    Last edited: May 21, 2020
  11. Storm1208

    Storm1208

    Joined:
    Nov 10, 2019
    Posts:
    5
    Awesome! Glad I was able to help dig up the issue!
     
  12. GrizzlyPunchGames

    GrizzlyPunchGames

    Joined:
    May 21, 2020
    Posts:
    11
    Hi,
    if you want i am currently creating top down shooting tutorial myself,
    here i have posted video where i am creating movement script:
     
  13. Cul0man

    Cul0man

    Joined:
    Mar 29, 2020
    Posts:
    1
    As of August 18 I'm following the same tutorial (Ruby's Adventure: 2D Beginner, awesome, by the way!) and I'm finding the same problem. While testing on Scene Ruby moves reaaaaaaaally slow. It's a bit hard to try colliders with GameObjects which are a bit far from her.
     
  14. huangmai

    huangmai

    Joined:
    Sep 20, 2019
    Posts:
    2
    It's a little bit late, but since I have the same problem and solved it in a way nobody mentioned yet, there is a easier solution to your problem.
    simply change Time.deltaTIme to Time.fixedDeltaTime in your original code and problem solved.
    This fix is better, cause from a coder's point of view, it's does feel weird to introduce 2 local vars just to store some infor you should to able to retrieved from properties.
    But it's your call anyway.
     
    lmayormoreno likes this.
  15. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    For those coming here with the same problem, here is a simple example of how to use RigidBody.MovePosition to move an object or character.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [RequiresComponent(typeof(RigidBody2D))]
    4. public class Controller : MonoBehaviour
    5. {
    6.     RigidBody2D rBody;
    7.     float moveSpeed = 2f;
    8.     Vector2 moveInput;
    9.  
    10.     void Awake()
    11.     {
    12.         rBody = GetComponent<RigidBody2D>();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         moveInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    18.     }
    19.  
    20.     void FixedUpdate()
    21.     {
    22.         Vector2 pos = rbody.Position + moveInput * moveSpeed * Time.fixedDeltaTime;
    23.         rBody.MovePosition(pos);
    24.     }
    25. }
    Do not use Update with MovePosition it will not work correctly. You can also use this in 3D with a few simple changes.

    @huangmai I'm not sure how your code is but if you are using Update and simply changing Time.deltaTime to Time.fixedDeltaTime, its still not working correctly. It will only accept the last value passed in before being processed in FixedUpdate. Also changing Time.deltaTime to Time.fixedDeltaTime does nothing to change what value is returned when used in FixedUpdate. The engine makes Time.deltaTime equal Time.fixedDeltaTime during FixedUpdate.
     
    MelvMay likes this.
  16. seamuss

    seamuss

    Joined:
    Oct 19, 2018
    Posts:
    13
    Hello. I am doing the 2D Ruby Adventure tutorial in Unity Learning page. It's october 2021. I have the same problem as the user you were replying a year ago. The code seems to not be updated or with some part missed. The code doesn't show the FixedUpdate function. Also, the character moves very slow after these changes.

    Could you please help me and report the problem?

    The code I can see in the tutorial is the next one (part 6 in tutorial, number 9 and 10):

    ruby tutorial code problem.png


    EDIT:
    It seems to work fine (at least for now) if you just change "Update" for "FixedUpdate". The character now seems to move correctly by doing this. So, the function "Update" should be renamed to function "FixedUpdate" with the same code inside. I would like to know if this is the correct or the "official" solution in the tutorial.
    Anyways, could you please report the problem? This tutorial is amazing and very helpful. It would be great if they correct this problem.
     
    Last edited: Oct 6, 2021
  17. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
  18. seamuss

    seamuss

    Joined:
    Oct 19, 2018
    Posts:
    13
    This is a capture of what I see (accessing the tutorial using the same link you posted), this is right before step 10, when they show the code:

    tutorial cap 1.png

    I am reading the tutorial in Spanish. Coud this be the problem? Maybe the image with the code is not updated in the Spanish course.

    I don't know if it is made with some automatic translation or not, and if the courses in different languages show the same files or not. But I mentioned that just in case.
     
  19. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    I have no idea why that would be the case in Spanish but I just posted your link on our internal "Learn" channel on the original thread I used before when it was fixed. They should know.
     
  20. seamuss

    seamuss

    Joined:
    Oct 19, 2018
    Posts:
    13
    Thank you very much.

    I just changed the language in my profile to English. Accessing again to the course, now I can see what you posted; the right code. So, somehow the problem seems to be that, the language of the course.

    tutorial cap 2.png
     
  21. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    Yes, I just did the opposite and changed my language to Spanish and sure enough I see what you reported: https://gyazo.com/4c6d5b25a0e2705e5fb981a0d499d17b

    I got a super quick reply from the learn team and indeed the localised versions are seperate. They told me they'd ask the localization team to get it updated.

    Thanks for reporting!
     
    seamuss likes this.
  22. seamuss

    seamuss

    Joined:
    Oct 19, 2018
    Posts:
    13
    Nice to know.
    Thank you for your help and your answers!
     
  23. DYJulian

    DYJulian

    Joined:
    May 18, 2022
    Posts:
    3
    This 'issue' is not fixed for Korean tutorial version as of March 7, 2023. It still shows Update function only. And Ruby mooooovs very slow.
    I hope the Korean tutorial version is also get fixed ASAP. Thanks.