Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 vetesting9 · Oct 03, 2018 at 07:19 AM · 2d game

How to make scrolling background to stop when player becomes idle?

I am new to unity. At present working on 2D game like Super Mario. Scrolling background is attached to moving camera. I want to make background to stop scroll when player becomes idle. My scrolling background script as below:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MovingBg : MonoBehaviour {
     
     public float scrollSpeed = 0.01f;
   
     void Update () {
 
          Vector2 offset = new Vector2(Time.time * scrollSpeed, 0);
       
         GetComponent<Renderer>().material.mainTextureOffset = offset;
  }
 
 }

Please help me out to solve the issue. Thank you

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by tormentoarmagedoom · Oct 03, 2018 at 07:49 AM

Good day.

You are moving the background with Time.time, which is the real time in the world. If you pretend to stop and play and stop and play the movement, you need some timer you can control, and not use the absolute Time.time

Easy way can be create your own timer, and stop/play the timer with player movement

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class MovingBg : MonoBehaviour {

  public float scrollSpeed = 0.01f;
  public float TimeForScroll = 0;

   
  void Update () {

 if (Player is moving)
   {
   TimeForScroll += Time.DeltaTime;
   }
  Vector2 offset = new Vector2(TimeForScroll  * scrollSpeed, 0);       
  GetComponent<Renderer>().material.mainTextureOffset = offset;

}

}

Something like this, so when the player does not move, TimeForScroll will not change so offset will not change.

Bye!

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
avatar image
1

Answer by Zodiarc · Oct 03, 2018 at 07:51 AM

Simply make the scrolling dependent on the player input. Use the same input that you use to move the player multiplied by -1 (since the background has to move in the opposite direction).

Using Input.GetAxis as an example:

 Vector2 offset = new Vector2(Time.time * Input.GetAxis("Horizontal")  *  -1 * scrollSpeed , 0);

But this alone will cause snapping back of the background. You also need to store the current offset value, so this would be my solution:

 public class Scroll: MonoBehaviour {
     public Vector2 offset = new Vector2(0,0);
     public float scrollSpeed = 1.0f;
     public void Update() {
         offset += new Vector2(Time.time * Input.GetAxis("Horizontal")  *  -1 * scrollSpeed , 0);
           //assign offset here
     }
 }


I'd also suggest using Time.deltaTime instead of Time.time. You don't have to work with extremely small floats then.

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
avatar image
0

Answer by vetesting9 · Oct 03, 2018 at 10:15 AM

Thank you guys for your reply. It worked fine for me.

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

105 People are following this question.

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

Related Questions

Moving a 2d player and preventing them from passing through objects 1 Answer

Having 2D Character fall when in collision 0 Answers

respawn all after player die or doing checkpoint 1 Answer

How to get touch.position in Unity "units"? 0 Answers

Enabling Sprite Renderer in C# 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