Question by
projojoboy · Mar 22, 2019 at 09:30 AM ·
2dphysicsunity 2dbackgroundparallax
Inverted parallax
I'm making a 2D game and want to add parallax, though because my player moves really fast the parallax is quite ugly. That's why I want to invert it so that when the player goes right the background goes right as well, but slower. The script I have does work but make the background glitch from left to right real fast.
[SerializeField] private float speed;
private Transform cam;
private Vector3 previousCamPos;
private void Start()
{
cam = Camera.main.transform;
previousCamPos = cam.position;
}
private void LateUpdate()
{
float parallax = (previousCamPos.x - cam.position.x);
float targetPosX = transform.position.x - parallax;
Vector3 targetPos = new Vector3(targetPosX, transform.position.y, transform.position.z);
transform.position = Vector3.Lerp(transform.position, targetPos, speed * Time.deltaTime);
previousCamPos = cam.position;
}
Comment
Your answer
Follow this Question
Related Questions
create a bouncy surface using c# unity2D 0 Answers
2D Parallax with zoom 0 Answers
2D Platform irregular jump 1 Answer
Why do prefabs overlap each other? 0 Answers
2D Physics movement system is creating unexplained random variations. 0 Answers