0
\$\begingroup\$

I'm new to Unity, so apologies for any oversight.

I have been trying to use the following code to create a parallax effect and an infinite background in Unity. It's from the following video: https://www.youtube.com/watch?v=wBol2xzxCOU.

using System.Collections.Generic;
using UnityEngine;

public class ParallaxBackground : MonoBehaviour
{

    [SerializeField] private Vector2 parallaxEffectMultiplier;
    [SerializeField] private bool infiniteHorizontal;
    [SerializeField] private bool infiniteVertical;

    private Transform cameraTransform;
    private Vector3 lastCameraPosition;
    private float textureUnitSizeX;
    private float textureUnitSizeY;

    private void Start()
    {
        cameraTransform = Camera.main.transform;
        lastCameraPosition = cameraTransform.position;
        Sprite sprite = GetComponent<SpriteRenderer>().sprite;
        Texture2D texture = sprite.texture;
        textureUnitSizeX = (texture.width / sprite.pixelsPerUnit) * transform.localScale.x;
        textureUnitSizeY = (texture.height / sprite.pixelsPerUnit) * transform.localScale.y;
    }

    private void FixedUpdate()
    {
        Vector3 deltaMovement = cameraTransform.position - lastCameraPosition;
        Debug.Log("PRINT DELTA: " + deltaMovement);
        transform.position += new Vector3(deltaMovement.x * parallaxEffectMultiplier.x, deltaMovement.y * parallaxEffectMultiplier.y);
        lastCameraPosition = cameraTransform.position;

        if (infiniteHorizontal)
        {
            if (Mathf.Abs(cameraTransform.position.x - transform.position.x) >= textureUnitSizeX)
            {
                float offsetPositionX = (cameraTransform.position.x - transform.position.x) % textureUnitSizeX;
                transform.position = new Vector3(cameraTransform.position.x + offsetPositionX, transform.position.y);
            }
        }

        if (infiniteVertical)
        {
            if (Mathf.Abs(cameraTransform.position.y - transform.position.y) >= textureUnitSizeY)
            {
                float offsetPositionY = (cameraTransform.position.y - transform.position.y) % textureUnitSizeY;
                transform.position = new Vector3(transform.position.x, cameraTransform.position.y + offsetPositionY);
            }
        }
    }

}

The idea is to capture the previous position of the camera, grab the delta, and then when the delta supercedes or equals the size of the texture, you then move that background texture over to the next place so it's always keeping up. The video provides more info.

I can't get this to work, because I don't seem to be capturing these camera position values correctly.

Debug:

  • I printed the camera positions (cameraTransform.position and lastCameraPosition) - despite what happens in FixedUpdate these two values are always the same, so delta is always 0

What I somehow need to do:

  • Somehow get the right values for camera position
  • Get the right delta printed out
  • Compare the delta with I believe the position and size of the texture that needs to be moved and repeated

I am using a Cinemachine virtual camera, using framing transposer, to follow my player across the screen. I know that this automatically positions the Unity camera relative to the player. I suspect this might be part of the problem, but then again, I don't see why, if the player starts at a certain place.

How do I fix this?

\$\endgroup\$
0

0

You must log in to answer this question.

Browse other questions tagged .