Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Sep 09, 2014 at 11:33 AM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
11
Question by $$anonymous$$ · Oct 09, 2013 at 10:18 AM · camera2dorthographicparallax

parallax scrolling using orthographic camera

Hi

I am making a 2d game and want multiple layers of parallax (maybe 5 layers).

Whats the best practice, move the player and camera on a static background, or keep the player still and move the background?

I thought maybe it would be a good optimization to do static batching on the background, which means it can't move, but because I'm using ortho not perspective camera, whats the best way to achieve this? Use a different camera for each parallax layer and have them follow the player camera at different speeds? Would there be any downsides to using this approach on mobile?

Many thanks

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

  • Sort: 
avatar image
25
Best Answer

Answer by cornel · Oct 28, 2013 at 03:08 PM

Here are two solutions: 1. Make a perspective camera (just one) and put your layers at different z positions. Then all you have to do is make this camera move when your ortho camera moves. Because it's a perspective camera the parallax layers will move at different speeds depending on how far they are from the camera. You can use static batching with this method since the layers are not moving in the world, only the camera moves. 2. Move the layers from script. Each layer should move at a different speed when your ortho camera moves. Here's what I've done:

 [ExecuteInEditMode]
 public class ParallaxBackground : MonoBehaviour
 {
   public ParallaxCamera parallaxCamera;
   List<ParallaxLayer> parallaxLayers = new List<ParallaxLayer>();
  
   void Start()
   {
       if (parallaxCamera == null)
         parallaxCamera = Camera.main.GetComponent<ParallaxCamera>();

       if (parallaxCamera != null)
         parallaxCamera.onCameraTranslate += Move;

       SetLayers();
   }
  
   void SetLayers()
   {
       parallaxLayers.Clear();

       for (int i = 0; i < transform.childCount; i++)
       {
           ParallaxLayer layer = transform.GetChild(i).GetComponent<ParallaxLayer>();
  
           if (layer != null)
           {
               layer.name = "Layer-" + i;
               parallaxLayers.Add(layer);
           }
       }
     }


     void Move(float delta)
     {
         foreach (ParallaxLayer layer in parallaxLayers)
       {
           layer.Move(delta);
       }
   }
 }

And:

 [ExecuteInEditMode]
 public class ParallaxLayer : MonoBehaviour
 {
       public float parallaxFactor;

       public void Move(float delta)
       {
           Vector3 newPos = transform.localPosition;
           newPos.x -= delta * parallaxFactor;

           transform.localPosition = newPos;
       }

 }

 [ExecuteInEditMode]
 public class ParallaxCamera : MonoBehaviour 
 {
     public delegate void ParallaxCameraDelegate(float deltaMovement);
     public ParallaxCameraDelegate onCameraTranslate;

     private float oldPosition;

     void Start()
     {
         oldPosition = transform.position.x;
     }

     void Update()
     {
         if (transform.position.x != oldPosition)
         {
             if (onCameraTranslate != null)
             {
                 float delta = oldPosition - transform.position.x;
                 onCameraTranslate(delta);
             }

             oldPosition = transform.position.x;
         }
     }
 }

How it works: First you have to have a gameobject with the ParallaxBackground script and add the layers as children to this game object and put the ParallaxLayer script on these. Then all you have to do is call Move(delta) where delta is the distance that the ortho camera moved. I made an event in the parallax camera, but you can do as you will. The layers will move with parallaxFactor ( if parallaxFactor is 1 then the layer will move at the same speed as the camera, if it's 0.5 then it will move 2 times slower, 2 for 2 times faster)

I prefer the second method since it's more 2d ish, but I have used both methods and both work well.

Comment
Add comment · Show 16 · 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 Invertex · Nov 07, 2013 at 05:58 AM 0
Share

Why not just put a script on each layer that links the layer's X position to your Ortho camera's X position, set to subtract it and multiplied by a speed of your choice.

avatar image cornel · Nov 07, 2013 at 07:16 AM 0
Share

Isn't that what I have done ?

avatar image Bonino · Dec 23, 2013 at 10:54 PM 0
Share

Just wanted to say thanks, this helped me out a lot!

avatar image cornel · Jan 08, 2014 at 08:41 AM 1
Share

This is how I have it: public static event Action onCameraTranslate; then I call it: if (onCameraTranslate != null) onCameraTranslate(deltaX); these are both in GameCamera

avatar image zero_null · Jan 02, 2018 at 12:35 PM 1
Share

What If I wanted to add a zoom effect. Like when the Player Jumps then the view-able area increases. The movement isn't very smooth, what if I just wanted to follow just a percent of the actual motion so that it don't cause motion sickness.

Show more comments

Follow this Question

Answers Answers and Comments

31 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

Related Questions

Why is part of object outside camera frustum visible? 1 Answer

Camera 2D RPG in perspective 1 Answer

Sprite relative position/scale problem with orthographic camera 0 Answers

ScreenToWorldPoint not accurate 0 Answers

2D camera zoom in comparison to the height of target 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