# Infinite Parallax Background with no lineal velocity

Hi all.

I have been investigating several ways to make an infinite parallax background in a 2D Project and I saw one I liked from Unity author and tutor Mike Geig Mike Geig on Youtube in which he uses the background sprites as textures on quads and move the textures using mainTextureOffset command.

``````void Update () {
renderer.material.mainTextureOffset = new Vector2 (Time.time * SpeedPercentage, 0f);
}
``````

I needed some tweaks as my game uses a ball that is thrown and bounces in the ground. Every time user clicks the ball adds some force to it. To simulate all this I use default Unity2D physics.

I wanted the background movement to adapt to my ball velocity so it would add a real sense of speed when the ball’s velocity is high. I changed the code to this one:

``````void Update () {
renderer.material.mainTextureOffset = new Vector2 (-Character.SpeedX * SpeedPercentage, 0f);
}
``````

Thought not perfect it worked fine at the begining… till it bounces.

Due to ball’s behaviour it, decreases it’s velocity each time it bounces, right now it maintains about 2/3 of it, and this affects texture’s offset, it “bounces” too as the variation is too big.

I have been thinking about possible solutions but I don’t find anything that can be made to allow a smooth transition from that sudden change in velocity.

Any ideas of how to handle this?

The image shows more or less what im experimenting right now

I’m not exactly sure what to do, but to try to stir some thoughts into your head I’ll give you my thoughts on this.

You need to determine the speed you want the texture to scroll at when the ball is at it’s maximum speed, which I’m assuming you have as SpeedPercentage? We should take the ball’s current speed and convert it into a range between 1 and 0 so that we can use it as a multiplier to affect the texture scrolling speed (1 is ball’s max speed, 0 is ball stopped). So that calculation would be:

``````currentSpeed / maxSpeed = scrollMultiplier;
``````

Then wouldn’t this be as simple as multiplying your texture scroll speed by this new “scrollMultiplier”?

Both the ball and the texture scrolling should both be affected by Time.deltaTime, so shouldn’t your new code just be:

``````void Update () {
renderer.material.mainTextureOffset = new Vector2 (-(Character.SpeedX/Character.MaxSpeedX) * SpeedPercentage * Time.deltaTime, 0f);
}
``````

Parallax background should be attached to the camera (the players view, not the ball)
You can set the orthographic camera to follow the ball, then implement some background layers that will parallax to match the camera movement.
Have a look at this project:

Hi im not quite sure i understand but gauging from what ive read, why not use “Momentum” as a variable instead of the balls actual speed.

example in pseudocode:

→ Ball speed changes
→ Momentum changes
→ Screen moves

this is to avoid the “jump” that you were talking about. The faster the ball speed the more momentum the screen move has. if the balls speed is 0 momentum will be decreased until it reaches 0.

Apologies if this was not “exactly” what you were looking.