1 year ago

#202497

test-img

ringazas

(Unity) Parallax background bug - tile map follows camera as well?

Asking this question due to not found solution anywhere else.

I created an infinite parallax scrolling background, but now wherever I go with character, the tile map follows me?

Can this be a bug due to Cinemachine package I'm using?

Here's my parallax script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Parallax : MonoBehaviour
{
    private float length, startpos;
    public GameObject cam;
    public float parallaxEffect;

    // Start is called before the first frame update
    void Start()
    {
        startpos = transform.position.x;
        length = GetComponent<SpriteRenderer>().bounds.size.x;
    }

    // Update is called once per frame
    void Update()
    {
        float temp = (cam.transform.position.x * (1 - parallaxEffect));
        float dist = (cam.transform.position.x * parallaxEffect);

        transform.position = new Vector3(startpos + dist, transform.position.y, transform.position.z);

        if (temp > startpos + length) startpos += length;
        else if (temp < startpos - length) startpos -= length;

    }
}

The tile map grid both has ground tags and layers, not in any way connected to the background.

If anyone can help me spot the issue, big thanks!

EDIT

I added images to describe the problem more clearly if anyone has issues.

See the transform X how the tile map follows the player as well.

unity-game-engine

camera

parallax

cinemachine

0 Answers

Your Answer

Accepted video resources