- Home /
Gap Between Scrolling Background Objects
Hi
I had this script down to where my scrolling background perfectly without gaps between background objects, but once I started changing around the background object dimensions, started giving some problems, eventhough I'm using the same basic formula.
I'm starting off with two cubes that I've resized so I can use them as scrolling background. I've resized them to 1.33x2.66 (1:2 aspect ratio) and the objective is to put a texture on it to be used as a background image. I would prefer to use 1x2 but I went back to my original dimensions just to troubleshoot, since this was working before. They are placed vertically so that the second object's lower edge touches the first object's top edge and is seamless. These two scroll downwards from top to bottom. The scrolling speed depends on user input, based on the character object's position. I'm using a playmaker FSM to control the scrolling speed.
The script provided here controls only the placement/instantiation of the prefab and also destroying of itself once it has reached the final position and instantiated the prefab.
Here's the code I'm using:
#pragma strict
var speed : float;
var error : float;
var position_y : float;
var Prefab_Path : String;
var transition_Bool : System.Boolean;
function Update () {
if (this.transform.position.y <= -1.6) {
transition_Bool = (Random.value > 0.5f);
if (transition_Bool == true) {
Prefab_Path = "Assets/CityStreets/Prefabs/Platform_Bridge.prefab";
}
else if (transition_Bool == false) {
Prefab_Path = "Assets/CityStreets/Prefabs/Platform.prefab";
}
//Now calculate the new position where you want to place the prefab
//But first, calculate the difference between the final position check (-1.6) and the current actual position
//error = transform.position.y + 1.6; this gives the difference (error)
//This will ultimately be used to prevent seams from appearing between each background tile
//3.72+1.6=5.32; 5.32 is the offset between the current tile position and the
//position where you want the new tile to spawn. The game starts off with
//2 tiles that are 2.66 units long each (5.32 units total)
//position_new of new spawn tile = (length of one tile x number of tiles) - cutoff position
// (where first tile disappears from camera view)
// + position error
//position_new = (2.66x2) - 1.6 + position error
error = this.transform.position.y + 1.6;
position_y = 3.72f + error;
//Sample: Instantiate(AssetDatabase.LoadAssetAtPath("Assets/MyPrefab.prefab", GameObject));
Instantiate(AssetDatabase.LoadAssetAtPath(Prefab_Path, GameObject), Vector3(0, position_y, 1), Quaternion.identity);
Destroy (gameObject);
}
}
Not sure why I'm still getting a small seam between background objects. It occurs every other instantiation. First instantiation has a seam, followed by one that is right where it's supposed to be (no seam), then one with a seam, and so on...I've attached a screenshot.
[1]: /storage/temp/15562-screenshot_tilegap.png
Your answer
Follow this Question
Related Questions
How to make sure spawned items match scrolling background speed? 0 Answers
Help with Generating/Spawning Background Object 2 Answers
instanstiate Spawns more then one objec 0 Answers
Insantiate object to parent without changing scale? 0 Answers
How to create an infinite scrolling background in top down multi-directional shooters. 2 Answers