Tuesday, June 7, 2022
HomeGame DevelopmentCreating an infinite scrolling background in Unity with Cinemachine

Creating an infinite scrolling background in Unity with Cinemachine


I am new to Unity, so apologies for any oversight.

I’ve been attempting to make use of the next code to create a parallax impact and an infinite background in Unity. It is from the next video: https://www.youtube.com/watch?v=wBol2xzxCOU.

utilizing System.Collections.Generic;
utilizing UnityEngine;

public class ParallaxBackground : MonoBehaviour
{

    [SerializeField] non-public Vector2 parallaxEffectMultiplier;
    [SerializeField] non-public bool infiniteHorizontal;
    [SerializeField] non-public bool infiniteVertical;

    non-public Rework cameraTransform;
    non-public Vector3 lastCameraPosition;
    non-public float textureUnitSizeX;
    non-public float textureUnitSizeY;

    non-public void Begin()
    {
        cameraTransform = Digital camera.primary.remodel;
        lastCameraPosition = cameraTransform.place;
        Sprite sprite = GetComponent<SpriteRenderer>().sprite;
        Texture2D texture = sprite.texture;
        textureUnitSizeX = (texture.width / sprite.pixelsPerUnit) * remodel.localScale.x;
        textureUnitSizeY = (texture.top / sprite.pixelsPerUnit) * remodel.localScale.y;
    }

    non-public void FixedUpdate()
    {
        Vector3 deltaMovement = cameraTransform.place - lastCameraPosition;
        Debug.Log("PRINT DELTA: " + deltaMovement);
        remodel.place += new Vector3(deltaMovement.x * parallaxEffectMultiplier.x, deltaMovement.y * parallaxEffectMultiplier.y);
        lastCameraPosition = cameraTransform.place;

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

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

}

The thought is to seize the earlier place of the digital camera, seize the delta, after which when the delta supercedes or equals the scale of the feel, you then transfer that background texture over to the subsequent place so it is at all times maintaining. The video supplies extra information.

I can not get this to work, as a result of I aren’t capturing these digital camera place values appropriately.

Debug:

  • I printed the digital camera positions (cameraTransform.place and lastCameraPosition) – regardless of what occurs in FixedUpdate these two values are at all times the identical, so delta is at all times 0

What I one way or the other must do:

  • One way or the other get the proper values for digital camera place
  • Get the proper delta printed out
  • Examine the delta with I consider the place and measurement of the feel that must be moved and repeated

I’m utilizing a Cinemachine digital digital camera, utilizing framing transposer, to comply with my participant throughout the display screen. I do know that this robotically positions the Unity digital camera relative to the participant. I believe this may be a part of the issue, however then once more, I do not see why, if the participant begins at a sure place.

How do I repair this?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments