- Home /
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:
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;
}
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.
Gosh, you completely right. However, when enabling the script, I can see the background moving, but it does not repeat as an infinite background.
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
Follow this Question
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