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
1
Question by paulaceccon · May 23, 2014 at 11:26 PM · 2dscrollingparallax

Vertical Parallax Scrolling

Hi, I'm wondering how I could do a vertical parallax scrolling. I know that this should be easy. However, this is my first time trying to do it and I'm having some difficulty.

I'm working with the 2D example provided with the new Unity version.

My background are sprites and my project looks like this:

alt text

After some research, I got to this code, which is attached to my Background game object, as the above image shows:

 public class BackgroundParallax : MonoBehaviour
 {
     public Transform[] backgrounds;  // Array of all the backgrounds to be parallaxed.
     public float speedY = 2f;
     
     void Update ()
     {
         // For each successive background...
         for(int i = 0; i < backgrounds.Length; i++)
         {
             Vector3 movement = new Vector3(0f, speedY, 0f);
             movement *= Time.deltaTime;
             backgrounds[i].transform.Translate(movement);
         }
     }
 }

However, I got no result with this scripts. Nothing happens and I'm wondering how to do this properly. What I'm trying to do is to do this sky sprites moves in the y direction, repeating these backgrounds (the env_2_Clouds are animated, and I don't know if I can repeat this too).

Thank you in advance.

EDIT:

How @MakeCodeNow correctly pointed, I made the dumb mistake of forgotten to enable the script. However, when enabling it, I can see the background going down, but it doesn't repeat, I just get blue sky after the clouds disappear.

I also tried this, which also result in nothing, nor even a single movement:

     public Transform[] backgrounds;                // Array of all the backgrounds to be parallaxed.
     public float parallaxScale;                    // The proportion of the camera's movement to move the backgrounds by.
     public float parallaxReductionFactor;        // How much less each successive layer should parallax.
     public float smoothing;                        // How smooth the parallax effect should be.
  
     private Transform cam;                        // Shorter reference to the main camera's transform.
     private Vector3 previousCamPos;                // The postion of the camera in the previous frame.
 
     void Awake ()
     {
         // Setting up the reference shortcut.
         cam = Camera.main.transform;
     }
    
     void Start ()
     {
         // The 'previous frame' had the current frame's camera position.
         previousCamPos = cam.position;
     }    
 
     void Update ()
     {
           // The parallax is the opposite of the camera movement since the previous frame multiplied by the scale.
         float parallax = (previousCamPos.y - cam.position.y) * parallaxScale;
 
         // For each successive background...
         for(int i = 0; i < backgrounds.Length; i++)
         {
             // ... set a target y position which is their current position plus the parallax multiplied by the reduction.
             float backgroundTargetPosY = backgrounds[i].position.y + parallax * (i * parallaxReductionFactor + 1);
 
             // Create a target position which is the background's current position but with it's target x position.
             Vector3 backgroundTargetPos = new Vector3(backgrounds[i].position.x, backgroundTargetPosY, backgrounds[i].position.z);
 
             // Lerp the background's position between itself and it's target position.
             backgrounds[i].position = Vector3.Lerp(backgrounds[i].position, backgroundTargetPos, smoothing * Time.deltaTime);
         }
 
         // Set the previousCamPos to the camera's position at the end of this frame.
         previousCamPos = cam.position;
     }

captura de tela 2014-05-23 às 20.16.58.png (86.3 kB)
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

1 Reply

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

Answer by MakeCodeNow · May 23, 2014 at 11:29 PM

The problem is that your BackgroundParallax script is disabled. Note that it's unchecked in the Inspector.

Well written question, btw. It's very easy to help with a good description and pictures.

Comment
Add comment · Show 2 · 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 paulaceccon · May 23, 2014 at 11:46 PM 0
Share

Gosh, you completely right. However, when enabling the script, I can see the background moving, but it does not repeat as an infinite background.

avatar image chris_alacris · May 24, 2014 at 03:45 AM 0
Share

Perhaps you could attempt to create two cloud game objects and stack them. Attach a script which would move the clouds down the desired length upon beco$$anonymous$$g invisible. void OnBecameInvisible() { transform.Translate( 0, (-2 * (renderer.bounds.size.y)), 0,Space.World); } What may also be interesting to change up the look is add some variation to the x position with a Random.Range(float, float). I hope this helps!

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

22 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

Related Questions

Parallax scrolling 1 Answer

2D Parallax Scrolling 1 Answer

parallax scrolling background 2 Answers

How to deal with 2D parallax in a building? 0 Answers

Unity 2D scrolling camera constantly 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