2D Platformer – Parallax going too fast in back

Updated on January 26, 2018 in [A] Brackeys Courses
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on January 8, 2017

Hey all, I recently got back into game creation and am working my way through Brackeys 2D Platformer course again.

However, I seem to have an issue with the parallaxing – it appears to go fast in the back and slow in the front, which should be the opposite of course. Any ideas what is going on? Could it have to do with any changes to Unity (like between 4.6 and 5) or the Standard Asset scripts since Brackeys made that tutorial?

Here’s a gif of what I mean: http://imgur.com/2hKc8ZA

Here’s my code. Any help would be appreciated, thanks!


using UnityEngine;

using System.Collections;
public class ParallaxScript : MonoBehaviour {
public Transform[] backgrounds; //Array (list) of all back- and foregrounds to be parallaxed

private float[] parallaxScales; //The proportion of the camera's movement to move backgrounds by

public float smoothing = 1f ; //How smooth the parallax is going to be, make sure it is above 0
private Transform cam; //Reference to the main camera's transform

private Vector3 previousCamPos; //The position of the camera in the previous frame
//Called before Start() but after game objects. (objs > awake > start) Great for storing references.

void Awake()

{

//set up the camera reference

cam = Camera.main.transform;

}
// Use this for initialization

void Start ()

{

//the previous frame had the current frame's camera position

previousCamPos = cam.position;
//assigning corresponding parallaxScales

parallaxScales = new float[backgrounds.Length];
//loop through backgrounds

for (int i = 0; i < backgrounds.Length; i++)

{

Debug.Log(backgrounds.Length);

//set background z-position to the corresponding scale (negative means moving left)

parallaxScales[i] = backgrounds[i].position.z * -1;

}
}
// Update is called once per frame

void Update ()

{

//for each background

for (int i = 0; i < backgrounds.Length; i++)

{

//get the movement change/amount (subtract the difference) multiplied by the scale

float parallax = (previousCamPos.x - cam.position.x) * parallaxScales[i];
//set a target x-position which is the current position plus the parallax

float backgroundTargetPosX = backgrounds[i].position.x + parallax;
//get a target position which is the background's current position + its target x position

Vector3 backgroundTargetPos = new Vector3(backgroundTargetPosX, backgrounds[i].position.y);
//fade between current position and target position using lerp

backgrounds[i].position = Vector3.Lerp(backgrounds[i].position, backgroundTargetPos, smoothing * Time.deltaTime);

}
//set the previousCamPos to the (current) cam position at the end of the frame

previousCamPos = cam.position;

}

}

  • Liked by
Reply
1 on January 9, 2017
On line 46 above here, you multiply with -1.
What happens if you do -1 / backgrounds[i].position.z?
 
If that doesn’t help, then I’d recommend to go over Brackeys code again to see if you have missed something.
on January 9, 2017

Hey Dion, thanks for responding. 🙂 I think the problem was actually with my arrangement of the camera and the backgrounds positioning to them. For whatever reason I didn’t think about that, but I believe I fixed the issue now. Thanks again.

Show more replies
  • Liked by
Reply
Cancel
0 on January 26, 2018

I’m having the same problem and I’d like for it to work like it does in the video, without having to reverse the order of the layers within the scene window. Did you ever figure out what the problem was so that you could arrange the layers in the actual order and have the parallax effect work properly? In the video tutorial, he doesn’t have to put the furthest layers closest to the camera like you and I have had to do.

@Dion Dokter, I tried dividing by -1 and using -1 as the divisor as well, and neither fixed the problem. Anyone else have any thoughts?

  • Liked by
Reply
Cancel