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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D movement stutter

Discussion in '2D' started by Teyhouse, Jun 10, 2014.

  1. Teyhouse

    Teyhouse

    Joined:
    May 25, 2013
    Posts:
    12
    Hi,

    i have got some performance-trouble while doing a simple task: Moving a single Sprite / Rigidbody2d over the screen. I simply took two sprites (one for the background and one for the moving object), placed them on the Scene and wrote a script to move one of the objects. The Code i used is like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class move : MonoBehaviour {
    5.  
    6.     Rigidbody2D car;
    7.     public Vector2 speed = new Vector2(10, 0);
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         car = this.rigidbody2D;
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void FixedUpdate () {
    16.         car.MovePosition(car.position + speed * Time.deltaTime);
    17.     }
    18. }
    The Sprite is stuttering - but why? I made you a video to clarify what i am talking about / attached the project. I would be realy thankful if somebody could check what's going wrong about my project. Thank you.

    Video: http://www.file-upload.net/download-9031974/Unity-2014-06-09-15-53-09-51.rar.html Project: Attached

    Regards
    Teyhouse
     

    Attached Files:

  2. cbothra

    cbothra

    Joined:
    Mar 14, 2014
    Posts:
    125
    Try LateUpdate() instead of FixedUpdate().
     
  3. Teyhouse

    Teyhouse

    Joined:
    May 25, 2013
    Posts:
    12
    Nope - that doesnt help. So guys before i get crazy to make sure i do not make something wrong: Please can somebody try it himself with the latest Unity-Version 4.5.0f6? Just make a new 2D project - place a sprite and let it move (only +x) from one site of the Camera to the other-site and look if you also got the stuttering-effect... if your version works pleace show it to me. This problem is getting kind of frustrating.
     
  4. Valette

    Valette

    Joined:
    Jun 9, 2014
    Posts:
    68
    Not sure if this helps (I'm pretty new to Unity) but this is what I got with your code. The part in the middle is with your code, where the car moves slowly and is noticeably jerky.



    I changed it to give the rigidbody2d some velocity and it seemed to work okay (the bits at the beginning and end), though the video makes it look a little less smooth than it does on my screen. Below, if it helps, is the code I used. The game I'm current working on uses rigidbodies all the time and I've never had a problem like this but I've also never moved them by position (I didn't even know the method existed until now). As far as I know, reading around the forums, I understood it was better to allow the rigidbodies to work out their places using their physics. I wonder if that's the problem here.

    Code (CSharp):
    1.      
    2.         Rigidbody2D car;
    3.         public Vector2 speed = new Vector2(10f, 0f);
    4.      
    5.         // Use this for initialization
    6.         void Start () {
    7.         car = gameObject.GetComponent<Rigidbody2D>();
    8.         car.velocity = speed;
    9.  
    10.     }
    11.      
    12.         // Update is called once per frame
    13.         void LateUpdate () {
    14.  
    15.         car.velocity = new Vector2(speed.x, car.velocity.y);
    16.  
    17.      
    18.     //        car.MovePosition(car.position + speed * Time.deltaTime);
    19.         }
    20.     }
     
  5. Teyhouse

    Teyhouse

    Joined:
    May 25, 2013
    Posts:
    12
    Nope, unfortunately that's not better
     
  6. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Could it be an fps issue ? You can try printing the fps and if its
    hovering around some value like 30, then its probably getting
    locked there. In that case, you can increase it with
    Application.targetFrameRate