- Home /
The Parallax goes way too fast
So Im trying to make an parallax backround with one background and then one foreground. I followed brackeys tutorial and have no idea of what I just wrote, Im pretty new and I understand certain parts, but not the overall connection between the codes. However, the background moves way too fast and when I change the smoothing value nothing noticable really happends so please help me :S
EDIT: the code's C#
using UnityEngine;
using System.Collections;
public class Parallaxing : MonoBehaviour {
public Transform[] backgrounds; // List of all the back and foreground to be parallaxed
private float[] parallaxScales; // The proportion of the camera's movement to move the backround by
public float smoothing = 1f; // How smooth the parallax is going to be. Make sure to set this above 0
private Transform cam; // Reference to the main cameras transform
private Vector3 previousCamPos; // the position of the camera in the previous frame
//Awake = Called before start
void Awake (){
//set up the camerarefrence
cam = Camera.main.transform;
}
// Use this for initialization
void Start () {
//the previous frame had the current frames camera position
previousCamPos = cam.position;
// assingning corespongding parallaxScales
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 each background
for (int i = 0; i < backgrounds.Length; i++){
// the parallax is the opposite of the cameramovement because the previous frame 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;
// creaye a target position which is the background's current position with it's target x position
Vector3 backgroundTargetPos = new Vector3 (backgroundTargetPosX, backgrounds[i].position.y,backgrounds[i].position.z);
// fade between current position and the target position using lerp
backgrounds[i].position = Vector3.Lerp (backgrounds[i].position, backgroundTargetPos, smoothing = Time.deltaTime);
}
// set the previousCamPos to the cameras position at the end of the frame
previousCamPos = cam.position;
}
}
Answer by iwaldrop · Jun 01, 2014 at 04:08 AM
I think the root of your problem lies on line 41, where you assign cam.position.x to previousCamPos.x instead of subtracting the former from the latter. The same goes for line 50 where you assign smoothing to Time.deltaTime.
Personally, I'd have a data structure that mated Transforms and speeds together, like so:
enter code hereusing UnityEngine;
using System.Collections;
public class Parallaxing : MonoBehaviour
{
public ParallaxObject[] objects;
private Vector3 previousCamPos; // the position of the camera in the previous frame
private Transform cam; // Reference to the main cameras transform
void Awake()
{
cam = Camera.main.transform;
}
void Start()
{
previousCamPos = cam.position;
if (objects.Length == 0)
{
enabled = false;
}
else
{
foreach (ParallaxObject po in objects)
po.SetParallaxScale();
}
}
void Update()
{
Vector3 deltaMovement = previousCamPos - cam.position;
previousCamPos = cam.position;
foreach (ParallaxObject po in objects)
po.Evaluate(deltaMovement);
}
}
[System.Serializable]
public class ParallaxObject
{
public Transform transform;
private Vector3 parallaxScale;
private float depth;
public void SetParallaxScale(float scale = -1)
{
depth = transform.position.z;
parallaxScale = (Vector3.right + Vector3.up) * depth * scale;
}
public void Evaluate(Vector3 cameraMovement)
{
Vector3 targetPosition = Vector3.zero;
for (int i = 0; i <= 2; i++)
cameraMovement[i] *= parallaxScale[i];
targetPosition = (transform.position + cameraMovement);
targetPosition.z = depth;
// fade between current position and the target position using lerp
transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime);
}
}
This allows you to add more options to each ParallaxObject as you need it. Honestly, I refactored the code so much since I started that I'd actually think a list of transforms and a helper function that took the parallaxed transform as a parameter would be better than another class which takes the camera delta as a parameter, but hopefully this gives you some idea.
The above code also parallaxes on the y axis as well. ;)