Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GenericEntity · Jul 12, 2015 at 10:54 AM · backgroundscrolling

How do I make a 2D scrolling background with variable speed?

Hello,

I've got a working 2D scrolling background, using the following code:

 public class Background : MonoBehaviour
 {
     public bool CanScroll { get; set; }
     public float Speed
     {
         get { return _speed; }
         set { _speed = value; }
     }
     public float StartingSpeed
     {
         get { return _startingSpeed; }
     }
 
     [SerializeField] private float _startingSpeed = 0.1F;
 
     private Renderer _renderer;
     private float _speed;
 
     protected void Awake()
     {
         _renderer = GetComponent<Renderer>();
         CanScroll = true;
         _speed = _startingSpeed;
     }
 
     protected void Update()
     {
         if (!CanScroll) return;
 
         // Pretend to be endlessly scrolling
         float y = Mathf.Repeat(Time.time * _speed, 1);
         Vector2 offset = new Vector2(0, -y);
         _renderer.sharedMaterial.SetTextureOffset("_MainTex", offset);
     }
 }

However, I want to be able to adjust _speed at runtime, and have it still scroll smoothly (or at least fake scrolling smoothly).

Currently when I adjust _speed at runtime, the background image will jump position very obviously, before switching to the new scrolling speed. And when I increase _speed in an Update loop, the background will suddenly scroll very quickly, when accelerating, then become slower when _speed stops increasing (that means when _speed is 15, it will be scrolling slower than when _speed is increasing, but at say 14.2, which doesn't make sense to me).

Ideally, I want to be able to increase and decrease _speed from another script's Update loop (through the Speed property) and have the background accelerate and decelerate smoothly.

Thanks for reading. Let me know if you need more information.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by timohausmann · Jul 13, 2015 at 01:22 AM

Here are some suggestions and an example that changes speed every 2 seconds with easing the speed.

  • don't use Time.time to multiply your speed. When you change speed, you will get unpredictable position jumpings. Check out Time.deltaTime to be framerate independent.

  • store the actual speed, the desired speed and the texture position as seperate variables to get more control.

  • I'm unsure about your double private/publics and the custom get/set methods. I will leave it out in this example.

public class Background : MonoBehaviour {

     public bool CanScroll;
     
     private Renderer _renderer;
     public float _speed = 0.1f; //current scrolling speed
     private float _desiredSpeed; //desired scrolling speed
     private float _pos; //total texture offset
     private float _lastChange; //just for demonstration
     
     protected void Awake()
     {
         _renderer = GetComponent<Renderer>();
         CanScroll = true;
 
         _desiredSpeed = _speed;
         _lastChange = 0; //just for demonstration
     }
     
     protected void Update()
     {
         if (!CanScroll) return;
 
         //just for demonstration: change desired speed
         if (Time.time > _lastChange + 2) {
             _desiredSpeed = Random.Range(-2f, 2f);
             _lastChange = Time.time;
             Debug.Log ("Set Speed To " + _desiredSpeed);
         }
 
         //this is where the magic happens: easing by hand!
         //Play with the value 0.01f to speed up or slow down the easing. 
         _speed += (_desiredSpeed - _speed) * 0.01f;
 
         //add current speed to the position
         _pos += _speed * Time.deltaTime;
         
         //for me it works without Mathf.Repeat
         //float y = Mathf.Repeat(_pos, 1);
         Vector2 offset = new Vector2(0, _pos);
         _renderer.sharedMaterial.SetTextureOffset("_MainTex", offset);
     }
 }

Hope this helps!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image t36k · Jun 07, 2016 at 12:09 AM 0
Share

Homeboy didn't say thanks, so I'll thank you for him. But you the real $$anonymous$$VP timohausmann!

avatar image balbinoaylagas · Jul 25, 2016 at 06:32 PM 0
Share

Thank you very much i had the same problem and this helped so much. The code is easy to use and understand.

avatar image King_Karl · Oct 03, 2016 at 02:53 AM 0
Share

Thanks! Really helped me out :D.

avatar image
0

Answer by nadhimali · Jul 12, 2015 at 02:00 PM

Try using a smooth damping Here to achieve smooth change in speed or use Lerp here

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

parallax scrolling background 2 Answers

Vertical camera scrolling fix? 0 Answers

Resolution Independent Scrolling Background 0 Answers

How do I make the scrolling texture background change direction depending on keyboard input for the player movement controls? 3 Answers

Perspective scrolling background 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges