0
\$\begingroup\$

What I am trying to accomplish:

  • I want to add a parallax effect to the background of my game.

  • I am working in 3D space with a perspective camera.


Why:

  • The reason that I want to add a parallax movement script even though I'm already using a perspective camera, is so I can keep all of my background layers the same scale.
  • Rather than dragging different layers further back in the Z-axis, I want to keep all of the layers at the same position in the z-axis so they remain uniform in scale.

The issue:

  • Currently, the code that I am using creates a working parallax effect.
  • However, the script is adding an offset in the x-axis to the game object it is attached to.
  • The offset amount is based on the variable 'parallaxEffectAmount'.
  • When parallaxEffectAmount is 0, the offset issue is gone and the layer is positioned at it's correct x-position in the scene. However, the parallax effect is gone.

The code I am currently using:

public class ParallaxScroll : MonoBehaviour
{

private float startPos;
[SerializeField] Camera cam;
[SerializeField] private float parallaxEffectAmount;

// Start is called before the first frame update
void Start()
{
    startPos = transform.position.x;
}

// Update is called once per frame
void Update()
{
    float distance = (cam.transform.position.x * parallaxEffectAmount);
    transform.position = new Vector3(startPos + distance, transform.position.y, transform.position.z);
}
}

Is there anyway I can implement a parallax effect for my background layers that will keep their original x-positions in the scene?

Does anyone have a better method of accomplishing this?

Thanks so much for taking the time!😀

\$\endgroup\$
4
  • \$\begingroup\$ And your cam is at 0,0,0 as well? \$\endgroup\$
    – Zibelas
    Jun 2, 2023 at 19:51
  • \$\begingroup\$ Thanks for the response! So, I use Cinemachine. My Main Camera's (cam) position is always the position of the Cinemachine camera being used. In this case, my camera that follows my hero at the start of the game. Because it's set to follow my game's hero, I can't actually drag the camera to (0,0,0). \$\endgroup\$ Jun 2, 2023 at 20:11
  • \$\begingroup\$ Can you add a screenshot of your scene hierarchy as well and mark where the script is located? \$\endgroup\$
    – Zibelas
    Jun 2, 2023 at 21:48
  • \$\begingroup\$ I'd be happy to! I've added a screenshot of the Hierarchy, listed where the script was located, and provided the positions. \$\endgroup\$ Jun 2, 2023 at 23:06

1 Answer 1

0
\$\begingroup\$

I unfortunately have no idea why the previous code I was using did not work. If anyone wants to chime in and explain it at any point, that would be terrific.

However, I went with a different approach and it works perfectly!

Hopefully this code will help someone doing something similar to me in the future:

public class ParallaxScroll1 : MonoBehaviour
{

    public float percentX;
    public float percentZ;
    [SerializeField] private Camera cam;
    Vector3 camPrev;


    // Start is called before the first frame update
    void Start()
    {
        camPrev = cam.transform.position;
    }

    // Update is called once per frame
    void LateUpdate()
    {
        Vector3 camPos = cam.transform.position;
        float deltaX = camPos.x - camPrev.x;
        float deltaZ = camPos.Z - camPrev.Z;

        float adjustX = deltaX * percentX;
        float adjustZ = deltaZ * percentZ;

        transform.position = transform.position + new Vector3(adjustX, 0, adjustZ);

        camPrev = camPos;
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .