Search Unity

Resolved Parallax And Screen Boxes

Discussion in 'Cinemachine' started by TheSpattt, Jan 11, 2023.

  1. TheSpattt

    TheSpattt

    Joined:
    Sep 3, 2019
    Posts:
    1
    Hello everyone,

    I'm having a bit of trouble with my parallax effect in my Unity project. Specifically, I want to disable the parallax effect when my screenbox (a UI element) is disabled, so that I can change from one screenbox to another without having everything become disorganized.

    I've tried using the GameObject.SetActive() method to set the active state of the parallax object to false when the screenbox is disabled, but that doesn't seem to be working. I also tried disabling the individual components (such as the script and image) that make up the parallax effect, but that didn't work either.

    I'm not sure what else to try, so I was hoping someone here might have some experience with this and could offer some advice. Has anyone run into a similar issue before? If so, what did you do to solve it?

    Any help would be greatly appreciated!

    Thank you in advance

    Here are the 4 Scripts I use, 3 For Parallax and 1 for the Screen Boxes (It's a 2D game on Unity 3D) :

    Code (CSharp):
    1. ParallaxLayer (goes on every Layer):
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. [ExecuteInEditMode]
    8. public class ParallaxLayer : MonoBehaviour
    9. {
    10.        public float parallaxFactor;
    11.        public void Move(float delta)
    12.        {
    13.            Vector3 newPos = transform.localPosition;
    14.            newPos.x -= delta * parallaxFactor;
    15.            transform.localPosition = newPos;
    16.        }
    17.  
    18.     private ParallaxLayer myScript;
    19.  
    20.     void Start()
    21.     {
    22.         myScript = GetComponent<ParallaxLayer>();
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         if (!gameObject.activeInHierarchy)
    28.         {
    29.             myScript.enabled = false;
    30.         }
    31.         else
    32.         {
    33.             myScript.enabled = true;
    34.         }
    35.     }
    36.  
    37. }
    38.  
    Code (CSharp):
    1. Parallax Camera (goes on the main camera):
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. [ExecuteInEditMode]
    8. public class ParallaxCamera : MonoBehaviour
    9. {
    10.      public delegate void ParallaxCameraDelegate(float deltaMovement);
    11.      public ParallaxCameraDelegate onCameraTranslate;
    12.      private float oldPosition;
    13.      void Start()
    14.      {
    15.          oldPosition = transform.position.x;
    16.      }
    17.      void LateUpdate()
    18.      {
    19.          if (transform.position.x != oldPosition)
    20.          {
    21.              if (onCameraTranslate != null)
    22.              {
    23.                  float delta = oldPosition - transform.position.x;
    24.                  onCameraTranslate(delta);
    25.              }
    26.              oldPosition = transform.position.x;
    27.          }
    28.      }
    29. }
    30.  
    Code (CSharp):
    1. Parallax Background (goes into an object that contain every Layer:
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. [ExecuteInEditMode]
    7.  
    8. public class ParallaxBackground : MonoBehaviour
    9. {
    10.    public ParallaxCamera parallaxCamera;
    11.    public List<ParallaxLayer> parallaxLayers = new List<ParallaxLayer>();
    12.  
    13.    void Start()
    14.    {
    15.        if (parallaxCamera == null)
    16.          parallaxCamera = Camera.main.GetComponent<ParallaxCamera>();
    17.        if (parallaxCamera != null)
    18.          parallaxCamera.onCameraTranslate += Move;
    19.        SetLayers();
    20.    }
    21.  
    22.    void SetLayers()
    23.    {
    24.        parallaxLayers.Clear();
    25.        for (int i = 0; i < transform.childCount; i++)
    26.        {
    27.            ParallaxLayer layer = transform.GetChild(i).GetComponent<ParallaxLayer>();
    28.  
    29.            if (layer != null)
    30.            {
    31.                layer.name = "Layer-" + i;
    32.                parallaxLayers.Add(layer);
    33.            }
    34.        }
    35.      }
    36.      void Move(float delta)
    37.      {
    38.          foreach (ParallaxLayer layer in parallaxLayers)
    39.        {
    40.            layer.Move(delta);
    41.        }
    42.    }
    43. }
    Code (CSharp):
    1. ScreenBox Manager (goes on an object that collide between the two screenboxes)
    2.  
    3. using UnityEngine;
    4. using System.Collections.Generic;
    5.  
    6. public class ScreenBoxManager : MonoBehaviour
    7. {
    8.     public GameObject frame1;
    9.     public GameObject frame2;
    10.    
    11.    
    12.     private void OnTriggerEnter2D(Collider2D collider){
    13.     if (frame1.active == true){
    14.         frame1.SetActive(false);
    15.         frame2.SetActive(true);
    16.         //disable parallax layers
    17.         var parallax = GameObject.Find("ParallaxBackground").GetComponent<ParallaxBackground>();
    18.         foreach (ParallaxLayer layer in parallax.parallaxLayers)
    19.         {
    20.             layer.gameObject.SetActive(false);
    21.         }
    22.     } else if (frame1.active == false){
    23.         frame1.SetActive(true);
    24.         frame2.SetActive(false);
    25.         //enable parallax layers
    26.         var parallax = GameObject.Find("ParallaxBackground").GetComponent<ParallaxBackground>();
    27.         foreach (ParallaxLayer layer in parallax.parallaxLayers)
    28.         {
    29.             layer.gameObject.SetActive(true);
    30.         }
    31.     }
    32. }
    33.  
    34. }
    35.  
    Hope you guys will be able to help me, Thank you !

    Spat'
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856