Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Scaling multiple sprites causes lag/stuttering

Discussion in '2D' started by jwennergren, Aug 12, 2019.

  1. jwennergren

    jwennergren

    Joined:
    Sep 21, 2018
    Posts:
    6
    Hi!

    I try to spawn 5 mushrooms(sprites) at random location and scale them up slowly. But when i do this i experience heavy stuttering. Why is this?

    Code (CSharp):
    1.     void LateUpdate()
    2.     {
    3.         // animate the position of the game object...
    4.         transform.localScale = new Vector3(transform.localScale.x + 0.01f, transform.localScale.y + 0.01f, 0);
    5.     }
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,768
    Probably framerate fluctuation?

    Currently you're scaling by .01 per frame. You want to be doing things independent from framerate so the hardware you run the game on doesn't influence the resulting gameplay. To do that, you can multiply by Time.deltaTime to make it "per second".

    Time.deltaTime is equal to the amount of time passed since last frame. It changes every frame as your framerate changes.

    Try this:
    Code (CSharp):
    1. public float scaleChangePerSecond = .1f;
    2. void LateUpdate()
    3. {
    4.     float scaleSpeed = scaleChangePerSecond * Time.deltaTime;
    5.     // animate the position of the game object...
    6.     transform.localScale = new Vector3(transform.localScale.x + scaleSpeed, transform.localScale.y + scaleSpeed, 0);
    7. }
     
    jwennergren likes this.
  3. jwennergren

    jwennergren

    Joined:
    Sep 21, 2018
    Posts:
    6
    Thanks for your reply.

    I tested to start a new project and scale the mushrooms up simultaneously. Now it works so i guess i must have hit some setting which causes the problem.

    I know i have fiddled with postprocessing and HDR in the previous project this might be the cause.
     
    LiterallyJeff likes this.
unityunity