- Home /
Scrolling background help - seams appearing...
Hi there,
I have the following code that I am using for a background that I want to scroll left and right, repeating. Unfortunately, I am getting a gap between the objects... sometimes it overlaps, other times there is the gap... is there any more precise way to scroll two objects as one?
private var fLeftPosition = -21.07282;
private var fRightPosition = 21.07282;
function Update ()
{
transform.localPosition.z = 5.785643; // Locks the Z position
// When reaches left limit, reset position
if (transform.localPosition.x < (fLeftPosition * 2))
{
transform.localPosition.x = (fRightPosition * 2);
}
// When reaches right limit, reset position
else if (transform.localPosition.x > (fRightPosition * 2))
{
transform.localPosition.x = (fLeftPosition * 2);
}
// Move background when turning left and right
if (PlayerControl.g_bPlayerOneIsTurningLeft == true)
{
transform.Translate((-PlayerControl.g_fRotationSpeed / 5) * Time.deltaTime, 0, 0);
}
else if (PlayerControl.g_bPlayerOneIsTurningRight == true)
{
transform.Translate((PlayerControl.g_fRotationSpeed / 5) * Time.deltaTime, 0, 0);
}
else
{
transform.Translate(0, 0, 0);
}
}
Hmmm... maybe I wasn't clear enough... I'm trying to scroll the background, so I have two planes. When one plane hits the far left position, it jumps back to its original position. The same happens when it scrolls the other way, but reversed. The problem I'm getting is that there seems to be a gap opening up between the two planes, even though speed is exactly the same. This process works on my 2D shooter, but in this more complicated game I'm having issues...
Answer by Joshua · Aug 17, 2011 at 09:50 PM
Instead of, when the object get's too far left/right directly setting the position back to the other far right/left position add the length difference to them.
What you're doing now is saying if you're too far left, meaning it's position is minLeft + a bit, go to far right. But the position of the second on is then offset by that 'a bit'.
So replace
transform.localPosition.x = (fRightPosition * 2);
with
transform.localPosition.x += (fRightPosition * 2)-(fLeftPosition * 2);
Thanks Joshua! I shall incorporate the code right away! Love this site ;-)
Haha glad it helped! Just fyi, if you're happy with an answer don't just accept it but also upvote it. It doesn't really matter for me, because I already have a high enough karma, but you might make others happy with it in the future ;)
I have found that animating the UVs works even better :)