Question by
robik_12 · Mar 31, 2017 at 10:46 PM ·
2dbackgroundscrollingparallaxortographic
2D Parallax with zoom
Hello. I have seen a lot of tutorials/answers about how to make a scroll parallax. But I have not seen the zoom part anywhere.
I created a small script but he's not working correctly. Movement is working properly, zoom in working properly, but if i try zoom out (to 0.1-0.3) and then moving - the position of the object becomes incorrect.
How to fix it?
using UnityEngine;
[ExecuteInEditMode]
public class ScrollParallax : MonoBehaviour {
public Camera Camera;
public float Zoom = 1;
public float ParallaxCoef;
private float moveParallaxCoef;
void Update()
{
moveParallaxCoef = Mathf.LerpUnclamped(1, 0, ParallaxCoef);
}
void LateUpdate()
{
var newSize = Mathf.Pow(Zoom, ParallaxCoef);
var newScale = new Vector2(newSize, newSize);
transform.localScale = newScale;
var newPos = (Vector2)Camera.transform.position * (1 - newSize + newSize * moveParallaxCoef);
transform.position = newPos;
}
}
Comment
Were you able to solve the issue ? Specially the zoom part ?