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

Jumpy movement with 2d physics

Discussion in 'Editor & General Support' started by LukePeek, Jul 17, 2014.

Thread Status:
Not open for further replies.
  1. LukePeek

    LukePeek

    Joined:
    Nov 29, 2013
    Posts:
    38
    I really didn't want to post this, the shear volume of topics about jumpy movement is insane, but I'm completely stumped and pretty frustrated!

    I have a very simple 2d scene, with a player (rigidbody2D) moving based on physics2d, on x at an increasing speed, and y in a wave-like motion using gravity and then turning gravity off and AddForce to move upwards, then turning gravity back on to move back down. (imagine a fish jumping in and out of water).

    The camera is following the fish, using simply transform.position and the scenery is all very slightly jumpy/stuttering. It's subtle but very noticeable.

    I've tried literally EVERY solution others have had success with but have had no luck. I think I've done something wrong in my movement scripts but I don't know what.

    Here is what I've already tried:

    1. Interpolate on player rigidbody - This makes the problem worse
    2. vsync off - again, this makes it worse
    3. using SmoothDamp on the camera instead of transform.position - This makes the scenery perfectly smooth, but the player is then the jumpy object, and about 4x as noticeable.
    4. Settings a constant velocity on the player instead of AddForce to gradually incrase - I thought it could be the dynamic speed causing issues, but no, I still get problems at a constant speed.
    5. Setting a fixed frame rate - no difference
    6. Setting the camera's movement in FixedUpdate instead of Update
    7. Setting the camera movement in LateUpdate instead of Update
    8. Setting the player velocity/addForce in Update instead of FixedUpdate

    The player is a gameobject containing a few sprites and the camera should follow it (with a bit of offset). The rest of the objects in the scene are completely static (for now, eventually it will be animated).


    Here are my movement scripts:

    Player
    Code (CSharp):
    1. void FixedUpdate()
    2. {
    3.     if ( running )
    4.     {
    5.         if ( rigidbody2D.velocity.x < targetVelocityX )
    6.         {
    7.             rigidbody2D.AddForce( Vector2.right * runForce );
    8.         } else {
    9.             rigidbody2D.velocity = new Vector2( targetVelocityX, 0 );
    10.         }
    11.  
    12.         if ( rigidbody2D.velocity.x > speedToJump ) {
    13.             // jump
    14.             rigidbody2D.velocity = new Vector2( rigidbody2D.velocity.x, maxVelocity );
    15.             rigidbody2D.gravityScale = 1;
    16.             running = false;
    17.         }
    18.  
    19.     }
    20.  
    21.     // limit y velocity
    22.     if ( rigidbody2D.velocity.y > maxVelocity ) rigidbody2D.velocity = new Vector2( rigidbody2D.velocity.x, maxVelocity );
    23.    //limit x
    24.    if ( rigidbody2D.velocity.x > targetVelocityX ) rigidbody2D.velocity = newVector2( targetVelocityX, rigidbody2D.velocity.y );
    25. }

    Camera
    Code (CSharp):
    1. void Update()
    2. {
    3.     transform.position = new Vector3( player.transform.position.x + offsetX, player.transform.position.y, player.transform.position.z );
    4. }

    Water
    Code (CSharp):
    1. void OnTriggerStay2D(Collider2D collider)
    2. {
    3.     if ( collider.tag == "Water" )
    4.     {
    5.         playerScript.inWater = true;
    6.         rigidbody2D.gravityScale = 0;
    7.  
    8.         rigidbody2D.AddForce( new Vector2( 2 , ( rigidbody2D.mass * buoyancy ) ) );
    9.     }
    10. }
    11.  
    12.  
    13. void OnTriggerExit2D(Collider2D collider)
    14. {
    15.     if ( collider.tag == "Water" ) {
    16.         playerScript.inWater = false;
    17.         rigidbody2D.gravityScale = 1;
    18.  
    19.     }
    20. }

    Thanks! :)
     
  2. Mike-Geig

    Mike-Geig

    Unity Technologies

    Joined:
    Aug 16, 2013
    Posts:
    220
    If you're having issues, the first thing I would do is avoid modifying velocity directly. Instead, I would just add a force every fixed frame to get the relative speed you'd like. See if that fixes your issue.
     
  3. LukePeek

    LukePeek

    Joined:
    Nov 29, 2013
    Posts:
    38
    Thanks Mike! Ok I'll give it go taking out all instances where I adjust velocity directly. So, for example, on line 14 of the player script where I make the player jump at a defined speed, I'll need to figure out what force I need to add to achieve that speed instead of setting the y velocity directly to it?
     
  4. LukePeek

    LukePeek

    Joined:
    Nov 29, 2013
    Posts:
    38
    I've replaced all instances of "rigidbody2d.velocity =" with "rigidbody2d.AddForce()" and it doesn't seem to have made any difference.
     
  5. Mike-Geig

    Mike-Geig

    Unity Technologies

    Joined:
    Aug 16, 2013
    Posts:
    220
    Sorry about the delay (the forums don't email me when there is a reply). Don't change the camera to the player immediately, try using Lerp instead.

    transform.position = Vector3.Lerp(target.position, transform.position, 5 * Time.deltaTime); <the 5 is arbitrary>
     
  6. PixellPat

    PixellPat

    Joined:
    Jun 13, 2021
    Posts:
    9
    Hey Luke, I am having the exact same issue as you, have you since solved the problem, maybe we can chat and I can show you the issue I'm having?
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,417
Thread Status:
Not open for further replies.