Background Parallax Effect (C# Script Included)


Unity 2D

I successfully incorporated a parallax effect into the background and made slight colour adjustments to enhance the forest theme. The parallax effect was achieved by creating a script that gains access to the camera's transform within the scene and then modifies the position of the game object (in this case, the different background image layers) using a corresponding multiplier. You can utilise the following script for this purpose:

Visit the game page here on itch.io and add it to your collection to stay up-to-date with updates and regular dev-logs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParallaxBackground : MonoBehaviour
{
    private Transform cameraTransform;
    private Vector3 lastCameraPosition;
    [SerializeField] [Tooltip("Further Behind = Greater Multiplier")]float parallaxEffecMultiplier;
    private void Start()
    {
        cameraTransform = Camera.main.transform;
        lastCameraPosition = cameraTransform.position;
    }
    private void LateUpdate()
    {
        Vector3 deltaMovement = cameraTransform.position - lastCameraPosition;
      
        // Background Transform Position
        transform.position += deltaMovement * parallaxEffecMultiplier;
        // Reset Last Camera Position to Current Camera Positon
        lastCameraPosition = cameraTransform.position;
    }
}

Leave a comment

Log in with itch.io to leave a comment.