- Home /
Moving Background with translated images
Hello, I have been having some trouble making a moving background that consists of images that represent waves.
What I want to do is have 2 images: Wave1 and Wave2.
I want to move them from right to left continously, by moving them and changing their position once they get off camera.
So it looks like Wave1 Wave2 (changeWave1 position) Wave1 Wave2 Wave1 Wave2 Wave1
I hope that is clear.
Anyway, All I do is move them with Lerp, and check their position relative to the Camera, if they get out of view, I change their positions from the left of the camera to the right of the camera, so they appear again on the right side of the camera. (exit on left, change position, appear on right).
That works well, the problem is when I move the camera (it follows a player), the script constantly checks if the wave is off camera, and moves it on camera again (relative to the cameras position of course), and SOME TIMES the position is not set right, and a little space appears between the wave images...
I need to fix this, is there a simpler way of doing what I'm doing? is that little white space due to the time it takes my computer to run the code?
Please help, I hope I made myself clear enough, as english is not my native language.
Answer by Loius · Jul 31, 2012 at 07:48 PM
Say your wave object is SIZE units in size.
if ( wave.transform.position.x < minimumX ) {
wave.transform.position.x += SIZE;
}
This will force your object to "align with itself"; if it has a repeating image it should line up properly. This is because you're setting it relative to itself so you know it will match its own graphics.
Oh, haha, I actually just built this for my own project. This one does some parallax. $$anonymous$$y objects are twenty units square, and each 'layer' is a gameobject containing a 3x3 grid of nine of those objects (as children).
#pragma strict
var layers : Transform[];
private var multipliers : float[];
function Start () {
multipliers = new float[ layers.length ];
multipliers[0] = 1;
for ( var ix : int = 1; ix < layers.length; ix++ ) {
multipliers[ix] = multipliers[ix-1] * 0.9;
}
StartCoroutine($$anonymous$$ove());
}
function $$anonymous$$ove() {
var ix : int;
while ( true ) {
for ( ix = 0; ix < layers.length; ix++ ) {
layers[ix].position += Game.backgroundSpeed * multipliers[ix] * Time.deltaTime;
if ( layers[ix].position.x < -10 ) layers[ix].position.x += 20;
if ( layers[ix].position.z < -10 ) layers[ix].position.z += 20;
if ( layers[ix].position.x > 10 ) layers[ix].position.x -= 20;
if ( layers[ix].position.z > 10 ) layers[ix].position.z -= 20;
}
yield;
}
}
Your answer
Follow this Question
Related Questions
parallax scrolling background 2 Answers
Parallax scrolling background performance problem 4 Answers
trouble with getting the background to show past the midground in 2d parallax backgrounds 0 Answers
2D Parallax Background 4 Answers
How to create an infinite scrolling background in top down multi-directional shooters. 2 Answers