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

2d platformer player jitter when add velocity

Discussion in '2D' started by kole_dole, Mar 28, 2020.

  1. kole_dole

    kole_dole

    Joined:
    Jul 8, 2018
    Posts:
    8
    Hi,

    I'm trying to make platformer player that moving forward and backward.

    Player is simple sprite

    Here is my code:

    private void FixedUpdate()
    {
    moveInput = Input.GetAxisRaw("Horizontal");
    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    }


    But when I try to move player I feel jitter (player not smooth moving) . Can problem be some settings in unity or problem is in code ?
    Also I try this code in Update and LateUpdate but still not smooth moving

    When I use follow player script player moves smooth but then camera jitter (not moving smooth) Here is my folow camera code:


    void FixedUpdate()
    {
    Vector3 playerPos = player.position;
    playerPos.z = transform.position.z;
    transform.position = Vector3.SmoothDamp(transform.position, playerPos, ref velocity, smoothTime);
    transform.position = new Vector3(Mathf.Clamp(transform.position.x, minCameraPos.x, maxCameraPos.x),Mathf.Clamp(transform.position.y, minCameraPos.y, maxCameraPos.y),
    Mathf.Clamp(transform.position.z, minCameraPos.z, maxCameraPos.z));
    }


    I create new simplest project with one scene, one script and code only for moving player , not camera follow and again player jitter, I build android apk and jitter againon my 8GB RAM phone .This is so weird

    Please help me. Thanks!
     

    Attached Files:

  2. Monique_Dumont

    Monique_Dumont

    Joined:
    Feb 18, 2020
    Posts:
    41
    Hello,

    It's maybe because you need to multiply the speed by Time.fixedDeltaTime.

    Also when posting code in Unity forums you should use the "insert code" functionnality like so :

    Code (CSharp):
    1. private void FixedUpdate()
    2. {
    3. moveInput = Input.GetAxisRaw("Horizontal");
    4. rb.velocity = new Vector2(moveInput * speed * Time.fixedDeltaTime, rb.velocity.y);
    5. }
    As a side note, you should take a look at the proper ways to move a 2D sprite if you're using or not using physics.
    Generally speaking I was told that you should not change the velocity directly.

    Hope this helps !
     
  3. kole_dole

    kole_dole

    Joined:
    Jul 8, 2018
    Posts:
    8
    I try with Time.fixedDeltaTime and same result again.
    I'm impossible to get a smooth motion in player movement. Is there any way to reset all Unity settings ? I sure that this is Unity problem.
    Its so simple code, and it doesn't work as expected.I read lot about FixedUpdate, Update...
    My previous game use the same code for movement and work fine.
     
  4. ZeroNineTwo

    ZeroNineTwo

    Joined:
    Nov 6, 2019
    Posts:
    1
    Your camera update should be in LateUpdate
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    Is there a reason why you are not using Interpolation? In your screenshot it's off which means you only want the Rigidbody2D to write to the Transform each fixed-update which isn't necessarily each frame and could be anything you've set but by default is 50hz.

    This is changing the Transform so do you know that changing the Transform is the primary purpose of the Rigidbody2D? By changing the Transform you're bypassing the Rigidbody2D completely overwriting what it's trying to do with the Transform. Also, you're doing that in the FixedUpdate so you don't get upated per-frame.

    It's easy to blame Unity and say this worked before but given what i've seen, I don't think that's the case at all.

    If you're using a Rigidbody2D then perform movement via its API and don't modify the Transform. You use a Dynamic body if you're going to do that via its velocity, forces etc and if you want the Rigidbody2D and its collider to react to collisions. If you want to control its position explicitly then set its body type to Kinematic and use Rigidbody2D.MovePosition to move it.

    In all cases of using Rigidbody2D and running the simulation during the fixed-update, turn on Interpolation. You don't need to do this if you run the simulation per-frame.
     
    Raial likes this.
  6. miharbiislam

    miharbiislam

    Joined:
    May 31, 2020
    Posts:
    1

    That was my problem, thank you so much. Movements alot more smooth now.
     
    MelvMay likes this.
  7. eldertechsystem

    eldertechsystem

    Joined:
    Aug 10, 2019
    Posts:
    2
    2d player run jitter, rb.velocity = vector2() in fixedUpdate() and interpolation is true, no camera follow player

    ==> Unity 2d game

    public class PlayerController : MonoBehaviour {
    private void Update() {
    moveInput = joystick.Horizontal;
    }
    void FixedUpdate() {
    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    }
    }

    ==> not camera follow to player

    ==> if camera follow player then player not jitter but background jitter, if camera set to Lear() then Player slightly jitter and background steel jitter. ==> try camera follow in LateUpdate() lightly jitter and FixedUpdate() more jittery

    ==> interpolate = interpolate in rigidbody2D porpertys
    ==> witout interpolate big jitter , set interpolat property steel smallest jitter in unity 2d player.
    ==>vsync count on or off steel smallest jitter on pc or android device. ==>not use more graphic in scenes just 5,6 images.
    ==> Please solve this problem else leave the unity
    ==> also player is not animated. it's just image.

    ==>Email : alpeshlilapara@gmail.com
    ==>Email : alpeshlilapara1@gmail.com
    ==>Email : Eldertechsystem@gmail.com

     
  8. eldertechsystem

    eldertechsystem

    Joined:
    Aug 10, 2019
    Posts:
    2

    In FixedUpdate No need multiply Time.fixedDeltaTime
     
  9. rogodoy

    rogodoy

    Joined:
    Jan 24, 2013
    Posts:
    21
    Set to interpolate in RigidBody, this fix the jittering. Also, in camera script > set to LateUpdate.