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.

Question 2D rigidbody falling object flickering

Discussion in '2D' started by Dekrud, Aug 24, 2023.

  1. Dekrud

    Dekrud

    Joined:
    Nov 9, 2017
    Posts:
    13
    Hello, I'm facing an issue and I don't know what else try. In my game some shapes are falling from the top of the screen. But they are like flickering all the way down. It is a 2d game and I've been changing several options I found on Google but nothing is improving this problem. The problem is more evident when I build the project and open it in an Android phone. In the editor is less evident.

    The falling shapes are a sprite with a rigidbody2d and a box collider 2d. The rigidbody has mass and gravity scale to 0 and interpolate is on interpolate.

    I'm moving the shapes in the fixedupdate method with
    Code (CSharp):
    1. rb.velocity = new Vector2(0, -fallSpeed);
    Fallspeed is a variable I increment over levels. I'm attaching a video so you can see what I see in my phone.

    Thanks in advanced for any possible help, I'm a bit desperate at this point.

    https://www.youtube.com/shorts/Z2gqKkQX8c0
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,811
    Well it's nothing to do with physics. Likely it's just stuff going on in the Editor causing player-loop hiccups. You can verify this by doing a build and running that.

    EDIT: You said it's worse in the Editor. That's bizarre!

    Try this on a near empty project. Try just moving it with transforms (no physics). Try setting the physics is simulation to run per-frame. Try using the profiler to see what's causing the spikes.
     
  3. Dekrud

    Dekrud

    Joined:
    Nov 9, 2017
    Posts:
    13
    Hey MelvMay thanks for your reply. I've said that in the editor is less evident but you can still notice the flickering. But in the mobile build is horrible, like in the video.

    I followed your tips. I've created a new empty project, using the 2D mobile core template. Switched platform from webgl to android. Added a 2D Object > Sprites > Triangle. Just added a collider not a rigidbody so no physics. I added a script called Move.cs and the only code for moving the object like falling from the top is:

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         transform.position += Vector3.down * 6f * Time.deltaTime;
    4.     }
    I just cloned 10 of this objects a created the build. Tested in two different smartphones and the result is the same: a big flickering effect.

    I don't understand why this happens.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,811
    I have no idea what's causing those performance hiccups then. I'd say you should attach the profiler to see if you can catch what's happening; clearly nothing to do with anything 2D.
     
  5. Dekrud

    Dekrud

    Joined:
    Nov 9, 2017
    Posts:
    13
    Well, after googling half of the internet and trying several things the one that worked in my case is adding this line:
    Code (CSharp):
    1.  Application.targetFrameRate = 60
    (add this in your awake or start method)
    I don´t know if it is the best solution but looks like by default Unity tends to set the frame rate at 30 for mobile apps but with that frame rate if you have things moving or a camera following the player (not my case) you get this flickering effects and it doesn't matter if you add interpolate to your rigidbodies or other options for compressing textures, sprites, etc. I´ve tried everything and that´s what worked.

    Also some people said to force vsync by adding
    Code (CSharp):
    1. QualitySettings.vSyncCount = 1;
    but this doesn't work for mobile because is disabled. The full explanation is in the Unity documentation.

    Hope this helps somebody else.
     
    MelvMay likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,811
    Yes, this needs to be set according to the device in question. Also note that physics is still driven at the rate defined by the time-manager so Time.fixedDeltaTime which, by default is 50Hz. This can be set to 60Hz or 30Hz depending on the frame-rate you're targetting. Also, physics can be run per-frame if you wish by setting the simulation mode in the Physics 2D settings (this removes the need for interpolation [it's ignored] and doesn't use FixedUpdate) but it means a variable rate simulation so reduces determinism but for a lot of games/projects this really doesn't matter.

    Just food-for-thought.
     
    Dekrud likes this.
  7. Dekrud

    Dekrud

    Joined:
    Nov 9, 2017
    Posts:
    13
    Thanks for this comment, gold knowledge ;)
     
    MelvMay likes this.
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,811
    You're most welcome.

    Good luck with your project!
     
    Dekrud likes this.
  9. Dekrud

    Dekrud

    Joined:
    Nov 9, 2017
    Posts:
    13
    MelvMay likes this.