- Home /
Making background move and loop
Hello guys,
I tried to find a solution for my problem, so these were the two I got. The first one was to create a quad, etc. I did not like it because I have a background sprite that covers the screen almost perfectly (it is a bit bigger).
What I did is to have 2 backgrounds, simple sprites that will rotate, but I get sometimes the problem of a blackline moving between the sprites. I would like to know how to fix this problem. I have tried to do it, with a code, but It worked half of the times. Sometimes the bug appeared, sometimes it didn't.
My camera does not move.
Here is my code using UnityEngine; using System.Collections;
public class MoveBackground : MonoBehaviour {
public float speed = 2;
public float maxSpeed = 0.5f;
private float speedscoreadd;
private float getBGspeed;
public float leftmax = 12;
//private int spriteNum;
public string levelDestroy;
public Vector3 rightspawn = new Vector3(12.5f, 0f, 1f);
private GameObject otherBG;
private ScoreKeeper scoreKeeper;
//public Sprite[] bgSprites;
Vector3 backgroundPos;
Vector3 otherBGPos;
// Use this for initialization
void Start () {
scoreKeeper = GameObject.Find ("ScoreKeeper").GetComponent<ScoreKeeper>();
if(this.name == ("Game Background 1")){
otherBG = GameObject.Find ("Game Background 2");
if (otherBG.name == "Game Background 2"){
Debug.Log ("Got Background 2 successfully");
}else{
Debug.Log ("I did not find the Second Background");
}
}
if(this.name == ("Game Background 2")){
otherBG = GameObject.Find ("Game Background 1");
if (otherBG.name == "Game Background 1"){
Debug.Log ("Got Background 1 successfully");
}else{
Debug.Log ("I did not find the First Background");
}
}
}
// Update is called once per frame
void FixedUpdate () {
if (Application.loadedLevelName == (levelDestroy)){
Destroy(this);
Debug.Log("Destroying duplicate instance");
}else{
DontDestroyOnLoad(this);
}
// speedscoreadd = scoreKeeper.score * 0.001f ; // if (speedscoreadd >= maxSpeed - speed) { // speedscoreadd = maxSpeed; // } // getBGspeed = -speed - speedscoreadd; // Debug.Log ("Speed background is:" + getBGspeed); transform.position += new Vector3( -speed - speedscoreadd,0,0); backgroundPos = this.transform.position; if (backgroundPos.x <= leftmax) { //spriteNum = Random.Range(0, 3); //this.GetComponent().sprite = bgSprites[spriteNum]; otherBGPos = new Vector3(otherBG.transform.position.x, 0, 0); if (otherBGPos.x <= 0){ Debug.Log ("Smaller"); this.transform.position = rightspawn + otherBGPos; }else if (otherBGPos.x > 0){ Debug.Log ("Bigger"); this.transform.position = rightspawn - otherBGPos; } } } }
If necessary I can send screen shots of how I made it all. I would really like some help,
Thanks in advance, Fritz
Your answer
Follow this Question
Related Questions
In my 2D platformer game, how would I create a height marker?, 1 Answer
How to check if an object hits the ground hard enough then add explosive force around it (2D) 1 Answer
Change the background color attribute of a camera in C#? 2 Answers
2D character gets stuck when falling 1 Answer
I cant get wall jumping to work in my 2D platformer (C#) 1 Answer