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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Consistent Stutter/Glitch in 2D movement

Discussion in '2D' started by SalvadorKinnes, Oct 29, 2019.

  1. SalvadorKinnes

    SalvadorKinnes

    Joined:
    Jun 1, 2019
    Posts:
    9
    Hi , I have been looking online for a solution ,to the constant stutter I get from moving a row of 5 columns to the left in my game, but have not found one. It is a side scrolling game.

    I am using void Update() { transform.position += Vector3.left * speed * Time.deltaTime; }

    The time settings in player settings are :
    Fixed Timestep = 0.02
    Max Allowed Timestep = 0.333333
    Time scale = 1
    Max Particle Timestep = 0.1

    I have checked the profiler and there are occasional render spikes. The camera is fixed and does not move. Vsync is also disabled , dont use that.

    The frame rate fluctuates anywhere between 150fps - 300fps. The platform is android. Batches called in total amount to 9/10 depending on gameplay. The CPU usage is max 4.5ms on average in the stats tab thing. I do not use any lighting or anything like that. Its really just a basic 2D game.

    The game uses about 250megs in the profiler. There are also rendering spikes in the memory profiler when the stutter occurs. I am a rookie at coding and am very new to this so please any help would be nice. I am also using unity 2019.1.4f1 Personal.

    I have tried using interpolation as well as fixed update method. Not sure what else to do so I have come to you. You help would be greatly appreciated

    I just want my game to run smoothly.
     
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    The movement code seems rather weak because you're adjusting the velocity to the left. I used to use this method of movement because it was quick and easy (works well with bullets and projectiles though) however, using velocity isn't a really good way to move after all as I've come to learn.

    Code (CSharp):
    1. private Rigidbody2D player;
    2. private float playerSpd = 5f;
    3. private Vector2 moveDirection;
    4.  
    5. void Start()
    6. {
    7. player = GetComponent<Rigidbody2D>();
    8. }
    9.  
    10. void Update()
    11. {
    12. moveDirection = Vector2.left; // Vector2(-1,0);
    13. }
    14.  
    15. void FixedUpdate()
    16. {
    17. player.MovePosition(player.position + (moveDirection * playerSpd) * Time.fixedDeltaTime;
    18. }
    So now you should be setting your player's velocity inside the Update() and then calculating its movement within the FixedUpdate() with smoothing with the Time.fixedDeltaTime.

    Now if you don't wish for non-stop movement, design a function and set a bool variable and an IF statement to summon the movement when you want.

    Edit - also using this method you can control and alter the speed by adjusting playerSpd so when you want to speed up or slow down, simply access that and change it. Set it to 0 or possibly moveDirection.zero may need to be applied. Still quick and easy.
     
    Last edited: Oct 29, 2019
  3. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Edit2 - I said velocity because I'm used to moving Rigidbody components with player.velocity = Vector2.left.
    However, I believe it's the same principal you are using transform.position = Vector3.left which is Vector3(-1,0,0) not really needed in 2D game design though. I'm just assuming you're using the rigidbody2D components velocity to move a 2D GameObject. Whether I'm right or wrong about it being the same thing as player.velocity, the code I provided should be correct.
     
    vakabaka likes this.
  4. SalvadorKinnes

    SalvadorKinnes

    Joined:
    Jun 1, 2019
    Posts:
    9
    Thanks for the help but it just made the stuttering worse, im not too sure why
     
  5. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    Do you have an object with collider and rigidbody attached ?

    the line just moves the object in the world without to check the physics. Also FixedUpdate and Interpolate wouldn't help here. This is used if something like a sprite (without the rigidbody and collider) should be moved in the world.

    For the objects with rigidbody and collider use physics. Something like MisterSkitz said.
    with the velocity this can be:
    Code (CSharp):
    1. Rigidbody2D rb;
    2.  
    3. void Start () {
    4. rb = GetComponent <Rigidbody2D>();
    5. }
    6.  
    7. void FixedUpdate () {
    8. rb.velocity = Vector2.left  * speed;
    9. }
    and here you can try to smooth it with the interpolate.

    Still I think the spikes have something with v-sync and max fps to do. Sorry, no idea here :eek:

    you can try to adjust the settings in the script:

    Code (CSharp):
    1. void Awake(){
    2. //try 0, 30, 60
    3. Application.targetFrameRate = 60;
    4. //try 0, 1, 2
    5. QualitySettings.vSyncCount = 0;
    6. }
     
    MisterSkitz likes this.
  6. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    May I see a screen shot of your player within the hierarchy? I'm wondering if you have multiple Rigidbodies attached on children. Another thing to take into account is if you're using particle system noise or other things that may affect performance. If you can make a video to demonstrate your issue, that would be great!
     
  7. SalvadorKinnes

    SalvadorKinnes

    Joined:
    Jun 1, 2019
    Posts:
    9
    Thank you for all the help , I figured out what was making it stutter. It was vsync that was disabled in the game mode.
     
    MisterSkitz likes this.
  8. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    No problem! And you just taught me something so I can now be aware of this issue. :)

    Glad you worked it out!