- Home /
 
               Question by 
               Coaster-Mind · May 02, 2016 at 08:32 PM · 
                c#2deffectsparallax  
              
 
              parallax on x, But how on the y as well? [Solved]
Hi.
I've made a script and it works. But my problem is that it only "scrolls?" on the x. And my question is, how do I need to make it so that it also gives this effect on the y?
 using UnityEngine;
 using System.Collections;
 
 public class Paralaxing : MonoBehaviour {
 
     public Transform[] Backgrounds;                //Array of background and forground
     private float[] ParallaxScales;
     public float Smoothing = 1f;                        //Minimal 1
 
     private Transform cam;
     private Vector3 PreCamPo;
 
     void Awake(){
         cam = Camera.main.transform;
     }
 
     // Use this for initialization
     void Start () {
         PreCamPo = cam.position;
 
         ParallaxScales = new float[Backgrounds.Length];
 
         for (int i = 0; i < Backgrounds.Length; i++) {
             ParallaxScales [i] = Backgrounds [i].position.z * -1;
         }
     }
     
     // Update is called once per frame
     void Update () {
 
         for (int i = 0; i < Backgrounds.Length; i++) {
             float parallax = (PreCamPo.x - cam.position.x) * ParallaxScales [i];
 
             float BackgroundTargetPosX = Backgrounds [i].position.x + parallax;
 
             Vector3 BackgroundTargetPos = new Vector3 (BackgroundTargetPosX, Backgrounds [i].position.y, Backgrounds [i].position.z);
 
             Backgrounds [i].position = Vector3.Lerp (Backgrounds [i].position, BackgroundTargetPos, Smoothing * Time.deltaTime);
 
 
         }
 
         PreCamPo = cam.position;
     }
 }
 
Hope it is an easy fix. Thanks.
-coasterMind
Script is halv copied from Brackeys.
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Cherno · May 02, 2016 at 09:20 PM
Probably you just have to mirror the code in Update() that does the effect for x:
 void Update () {
  
          for (int i = 0; i < Backgrounds.Length; i++) {
              float parallax = (PreCamPo.x - cam.position.x) * ParallaxScales [i];
  
              float BackgroundTargetPosX = Backgrounds [i].position.x + parallax;
  
              Vector3 BackgroundTargetPos = new Vector3 (BackgroundTargetPosX, Backgrounds [i].position.y, Backgrounds [i].position.z);
  
              Backgrounds [i].position = Vector3.Lerp (Backgrounds [i].position, BackgroundTargetPos, Smoothing * Time.deltaTime);
  
  
          }
Copy everything that is specific to x and use it again for y.
 void Update () {
      
              for (int i = 0; i < Backgrounds.Length; i++) {
                  float parallax_x = (PreCamPo.x - cam.position.x) * ParallaxScales [i];
                  float parallax_y = (PreCamPo.y - cam.position.y) * ParallaxScales [i];
      
                  float BackgroundTargetPosX = Backgrounds [i].position.x + parallax_x;
                   float BackgroundTargetPosY = Backgrounds [i].position.y + parallax_y;
      
                  Vector3 BackgroundTargetPos = new Vector3 (BackgroundTargetPosX, BackgroundTargetPosY, Backgrounds [i].position.z);
      
                  Backgrounds [i].position = Vector3.Lerp (Backgrounds [i].position, BackgroundTargetPos, Smoothing * Time.deltaTime);
      
      
              }
Thanks so much! It worked! can you post this as an answer so I can mark it green?
Your answer
 
 
             Follow this Question
Related Questions
3D Background Effect And Dynamic Parallax 1 Answer
Multiple Cars not working 1 Answer
How do I do 2d parallax 1 Answer
Distribute terrain in zones 3 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                