Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Pavan Srivathsa · Aug 11, 2014 at 05:58 PM · c#scrolling

how to scroll backgrounds from left to right

below is the scrolling script which allows bgs to move right to left can u plz tell me where to modify it to scroll from left to right

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

/// /// Parallax scrolling script that should be assigned to a layer /// public class ScrollingScript : MonoBehaviour { ///

/// Scrolling speed /// public Vector2 speed = new Vector2(10, 10);

 /// <summary>
 /// Moving direction
 /// </summary>
 public Vector2 direction = new Vector2(-1, 0);

 /// <summary>
 /// Movement should be applied to camera
 /// </summary>
 public bool isLinkedToCamera = false;

 /// <summary>
 /// 1 - Background is infinite
 /// </summary>
 public bool isLooping = false;

 /// <summary>
 /// 2 - List of children with a renderer.
 /// </summary>
 private List<Transform> backgroundPart;

 // 3 - Get all the children
 void Start()
 {
     // For infinite background only
     if (isLooping)
     {
         // Get all the children of the layer with a renderer
         backgroundPart = new List<Transform>();

         for (int i = 0; i < transform.childCount; i++)
         {
             Transform child = transform.GetChild(i);

             // Add only the visible children
             if (child.renderer != null)
             {
                 backgroundPart.Add(child);
             }
         }

         // Sort by position.
         // Note: Get the children from left to right.
         // We would need to add a few conditions to handle
         // all the possible scrolling directions.
         backgroundPart = backgroundPart.OrderBy(
           t => t.position.x
         ).ToList();
     }
 }

 void Update()
 {
     // Movement
     Vector3 movement = new Vector3(
       speed.x * direction.x,
       speed.y * direction.y,
       0);

     movement *= Time.deltaTime;
     transform.Translate(movement);

     // Move the camera
     if (isLinkedToCamera)
     {
         Camera.main.transform.Translate(movement);
     }

     // 4 - Loop
     if (isLooping)
     {
         // Get the first object.
         // The list is ordered from left (x position) to right.
         Transform firstChild = backgroundPart.FirstOrDefault();

         if (firstChild != null)
         {
             // Check if the child is already (partly) before the camera.
             // We test the position first because the IsVisibleFrom
             // method is a bit heavier to execute.
             if (firstChild.position.x < Camera.main.transform.position.x)
             {
                 // If the child is already on the left of the camera,
                 // we test if it's completely outside and needs to be
                 // recycled.
                 if (firstChild.renderer.IsVisibleFrom(Camera.main) == false)
                 {
                     // Get the last child position.
                     Transform lastChild = backgroundPart.LastOrDefault();
                     Vector3 lastPosition = lastChild.transform.position;
                     Vector3 lastSize = (lastChild.renderer.bounds.max - lastChild.renderer.bounds.min);

                     // Set the position of the recyled one to be AFTER
                     // the last child.
                     // Note: Only work for horizontal scrolling currently.
                     firstChild.position = new Vector3(lastPosition.x + lastSize.x, firstChild.position.y, firstChild.position.z);

                     // Set the recycled child to the last position
                     // of the backgroundPart list.
                     backgroundPart.Remove(firstChild);
                     backgroundPart.Add(firstChild);
                 }
             }
         }
     }
 }

}

Comment
Add comment · Show 4
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 robertbu · Aug 11, 2014 at 06:03 PM 0
Share

Just set direction in inspector. Change -1 to 1

avatar image Pavan Srivathsa robertbu · Aug 11, 2014 at 06:27 PM 0
Share

yeah I have tried it but just one texture is moving in d desired direction but no looping is there

avatar image robertbu · Aug 11, 2014 at 06:59 PM 0
Share

I converted my answer to a comment. If you don't get an answer, I'll play with it a bit and figure out the solution when I get a bit of time, but I would need code for the Renderer extention method 'IsVisbieFrom()'?

avatar image Shark-Boy · Aug 11, 2014 at 07:40 PM 0
Share

Ha ha. So this script seems kind of familiar . Oh maybe I did that tutorial too. So robertbu asked for the Renderer extention method. Here it is.

 using UnityEngine;
 
 public static class RendererExtensions
 {
     public static bool IsVisibleFrom(this Renderer renderer, Camera camera)
     {
         Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
         return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
     }
 }

If I could answer the question I would but I tried modifying the scrolling script and I kind of failed. Hope this question gets answered soon because now I want to know how to do it.

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by titulus · Aug 11, 2014 at 09:19 PM

At first view, I would say that looping in left to right direction is impossible with this script. But modify the script in the following way might help:

 using System.Collections.Generic; using System.Linq; using UnityEngine;
 
 /// /// Parallax scrolling script that should be assigned to a layer /// public class ScrollingScript : MonoBehaviour { ///
 /// Scrolling speed /// public Vector2 speed = new Vector2(10, 10);
 
     /// <summary>
     /// Moving direction
     /// </summary>
     public Vector2 direction = new Vector2(1, 0); // move left to right
      
     /// <summary>
     /// Movement should be applied to camera
     /// </summary>
     public bool isLinkedToCamera = false;
      
     /// <summary>
     /// 1 - Background is infinite
     /// </summary>
     public bool isLooping = false;
      
     /// <summary>
     /// 2 - List of children with a renderer.
     /// </summary>
     private List<Transform> backgroundPart;
      
     // 3 - Get all the children
     void Start()
     {
     // For infinite background only
     if (isLooping)
     {
     // Get all the children of the layer with a renderer
     backgroundPart = new List<Transform>();
      
     for (int i = 0; i < transform.childCount; i++)
     {
     Transform child = transform.GetChild(i);
      
     // Add only the visible children
     if (child.renderer != null)
     {
     backgroundPart.Add(child);
     }
     }
      
     // Sort by position.
     // Note: Get the children from left to right.
     // We would need to add a few conditions to handle
     // all the possible scrolling directions.
     backgroundPart = backgroundPart.OrderByDescending( // order the children right to left
     t => t.position.x
     ).ToList();
     }
     }
      
     void Update()
     {
     // Movement
     Vector3 movement = new Vector3(
     speed.x * direction.x,
     speed.y * direction.y,
     0);
      
     movement *= Time.deltaTime;
     transform.Translate(movement);
      
     // Move the camera
     if (isLinkedToCamera)
     {
     Camera.main.transform.Translate(movement);
     }
      
     // 4 - Loop
     if (isLooping)
     {
     // Get the first object.
     // The list is ordered from left (x position) to right.
     Transform firstChild = backgroundPart.FirstOrDefault();
      
     if (firstChild != null)
     {
     // Check if the child is already (partly) before the camera.
     // We test the position first because the IsVisibleFrom
     // method is a bit heavier to execute.
     if (firstChild.position.x > Camera.main.transform.position.x) // if child is left side of the camera
     {
     // If the child is already on the left of the camera,
     // we test if it's completely outside and needs to be
     // recycled.
     if (firstChild.renderer.IsVisibleFrom(Camera.main) == false)
     {
     // Get the last child position.
     Transform lastChild = backgroundPart.LastOrDefault();
     Vector3 lastPosition = lastChild.transform.position;
     Vector3 lastSize = (lastChild.renderer.bounds.max - lastChild.renderer.bounds.min);
      
     // Set the position of the recyled one to be AFTER
     // the last child.
     // Note: Only work for horizontal scrolling currently.
     firstChild.position = new Vector3(lastPosition.x - lastSize.x, firstChild.position.y, firstChild.position.z); // add the firstChild on the left side of the lastChild
      
     // Set the recycled child to the last position
     // of the backgroundPart list.
     backgroundPart.Remove(firstChild);
     backgroundPart.Add(firstChild);
     }
     }
     }
     }
     }

I have not compile or test this script.

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 smoggach · Aug 11, 2014 at 09:04 PM

Never hurts to read the comments :) props to the original author.

  // Sort by position.
 // Note: Get the children from left to right.
 // We would need to add a few conditions to handle
 // all the possible scrolling directions.
 
 if (direction.x < 0)
 {
     backgroundPart = backgroundPart.OrderBy(
     t => t.position.x
     ).ToList();
 }
 else
 {
     backgroundPart = backgroundPart.OrderByDescending(
     t => t.position.x
     ).ToList();
 }






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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Scrolling Image with no renderer and no SetTextureOffset 0 Answers

Menu scrolling with Vive Touchpad? 2 Answers


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