Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Stuttering while moving Background

Discussion in 'Scripting' started by Rphysx, Apr 20, 2014.

  1. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    So I have a 2D character that moves in the screen with GetAxisRaw function and i'm for testing purposes relying on the update() method to move the char itself. Many 2D games have somesort of script that I tried to recreate that moves the various layers of the background at different speed, recreating thus a cool 3d looking environment while player moves.
    Apparently I got how to move different background layers at different speed and all of this works except for the immense stuttering that I see at higher movement speed.

    Code (csharp):
    1.  
    2.  
    3. void Update ()
    4.     {
    5.         //Character Movement
    6.         GetAxis = Input.GetAxisRaw("Horizontal");          
    7.         rigidbody2D.velocity = new Vector2(GetAxis * Velocity, rigidbody2D.velocity.y);
    8.  
    9.         //if scrollbackground's set to true the following gets executed
    10.         if (ScrollBackground)
    11.         {
    12.             Background.transform.position = new Vector3(Background.transform.position.x + GetAxis * Velocity / 100, Background.transform.position.y, 5f);
    13.         }
    14.     }
    15.  
    feel free to ask for more details if the above seems right to you

    IMPORTANT EDIT : this problem occurs if character movement is set with rigidbody2d.velocity property. changing it to a transform.position style movement would fix this, but I need the rigidbody in any case, any idea ?
     
    Last edited: Apr 20, 2014
  2. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    First off, try FixedUpdate rather than Update. Since Update is called every frame, your background will actually move faster at higher frame rates. Not necessarily desirable. This might solve some of your problem. FixedUpdate is called at regular intervals, rather than every single frame.

    It might be that what's happening, or at least some of it, is that all of those quick transform calls are adding up to a decent little bit of strain on the processing system. Calling it all a little less frequently could reduce that strain. For a better solution, at higher velocities you could handle it in FixedUpdate, and handle lower velocities in Update.
     
    jeremianoel2244 likes this.
  3. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    Already tried it, the problem is still happening. I believe that this stuttering is caused by some part of the script that "teleports" the background back to it's original position and then at the end of the frame gets moved again. I can't really figure out which part of my code could do this "teleport back" neither i'm sure that this is happening
     
  4. wondyr

    wondyr

    Joined:
    Nov 30, 2013
    Posts:
    36
    Assuming your background is a parallax background try this:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class GenericScrollScript: MonoBehaviour
    4. {
    5.     public float speed = 0;
    6.    
    7.     void Update()
    8.     {
    9.         renderer.material.mainTextureOffset = new Vector2((Time.time * speed)%1, 0f);
    10.     }
    11. }
     
  5. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    Actually works thank you
     
  6. Bud-Leiser

    Bud-Leiser

    Joined:
    Aug 21, 2013
    Posts:
    93
    Just wanted to say thank you and if anyone else is looking for a moving background script this is what you need!