Search Unity

Help with level backgrounds set up using parallax

Discussion in '2D' started by rfuhrer, Jul 23, 2016.

  1. rfuhrer

    rfuhrer

    Joined:
    Oct 2, 2015
    Posts:
    52
    Hey everyone,

    I have some issues with seting up my background elements in my levels.
    My levels are quite long horizontaly and verticaly I am using different layers with different background elements all scroll with different speeds using (EZ parallax asset) parallax.

    Right now it is difficult to set up elements that are far away from the start point since I can not position them properly when the scene is not running since they get moved away trough the parallax effect.

    Does anyone know a tool, or has any tips how to set that up the best way?

    Any help is highly appreciated.
     
  2. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    Are you running the game inside of Unity with the maximize screen option checked? I would have the Game view not maximized and during run time (while the game is being played), just zoom out with the mouse wheel and move around with middle click in the Scene view window.
     
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I ran into the same problem. The way I solved it was using the [ExecuteInEditMode] attribute on my parallax layer class. That way they update in the editor relative to the camera. That allows me to move the camera around and preview the movement and positioning when the camera is at different spots in the level without having to play and adjust again and again.

    I also created a script for my camera that shows two buttons in the inspector, one to store the current camera position, and one to restore it. That way I was able to adjust things to look right down the line without losing my original position.


    The alternative is to play the game, and move an element to make it look right when the camera is at a certain spot. Then stop playing and set it to that position. You can copy the transform component or the whole object.
     
    Last edited: Jul 25, 2016
  4. rfuhrer

    rfuhrer

    Joined:
    Oct 2, 2015
    Posts:
    52
    @unitynoob24 Well that part is not the problem! I can see and manipulate the stuff in the scene mode while playing but it gets reverted when stoping the play mode

    @jeffreyschoch thanks for your suggestion I will see if I can set up something similar. But with my parallax script it is not just the camera but also the player that has influence on the position.

    It would be cool, if it could be positioned in the scene in editor mode and it gets calculated to the position where it needs to be. I mean if I am not wrong it should just do the calculation backwards, right?
     
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    As long as the objects that are moving relative to the camera & player are set up to update correctly, they should be able to move themselves at editortime just like during the game. The only problems will occur if there are other things that are required happen at runtime in order for it to work.

    My layers move relative to the camera every frame. The layer script keeps track of its change in position each frame, and the layers read this value each frame and move the same amount, multiplied by their respective parallax value. So moving backwards would be the same operation. If the camera position changes, the layers' positions change too.

    Here's my current setup (i wrote this a long time ago, so it may not be fully optimized):

    On the parent object acting as the layer root:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. public class ParallaxLayer : MonoBehaviour {
    5.  
    6.     public Vector2 speed;
    7.     public bool moveInOppositeDirection;
    8.  
    9.     private Vector3 previousCameraPosition;
    10.     private bool previousMoveParallax;
    11.     private ParallaxOption options;
    12.  
    13.     private void OnEnable()
    14.     {
    15.         options = Camera.main.GetComponent<ParallaxOption>();
    16.         previousCameraPosition = Camera.main.transform.position;
    17.     }
    18.  
    19.     void Update ()
    20.     {
    21.         if (options.moveParallax && !previousMoveParallax)
    22.         {
    23.             previousCameraPosition = Camera.main.transform.position;
    24.         }
    25.  
    26.         previousMoveParallax = options.moveParallax;
    27.  
    28.         if (!Application.isPlaying && !options.moveParallax)
    29.         {
    30.             return;
    31.         }
    32.  
    33.         Vector3 distance = Camera.main.transform.position - previousCameraPosition;
    34.         float direction = (moveInOppositeDirection) ? -1f : 1f;
    35.         transform.position += Vector3.Scale(distance, new Vector3(speed.x, speed.y)) * direction;
    36.  
    37.         previousCameraPosition = Camera.main.transform.position;
    38.     }
    39. }
    on the camera:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ParallaxOption : MonoBehaviour {
    5.  
    6.     public bool moveParallax;
    7.  
    8.     [SerializeField]
    9.     [HideInInspector]
    10.     private Vector3 storedPosition;
    11.  
    12.     public void SavePosition() {
    13.         storedPosition = transform.position;
    14.     }
    15.  
    16.     public void RestorePosition() {
    17.         transform.position = storedPosition;
    18.     }
    19. }
    In a folder in the project named "Editor":
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomEditor(typeof(ParallaxOption))]
    5. class ParallaxOptionEditor : Editor
    6. {
    7.     private ParallaxOption options;
    8.  
    9.     void Awake()
    10.     {
    11.         options = (ParallaxOption)target;
    12.     }
    13.  
    14.     public override void OnInspectorGUI() {
    15.         DrawDefaultInspector();
    16.  
    17.         if(GUILayout.Button("Save Position"))
    18.         {
    19.             options.SavePosition();
    20.             EditorUtility.SetDirty(options);
    21.         }
    22.  
    23.         if(GUILayout.Button("Restore Position"))
    24.         {
    25.             options.RestorePosition();
    26.         }
    27.     }
    28. }
     
    Last edited: Jul 26, 2016