- Home /
Question by
SmileApplication · Jan 17, 2014 at 03:39 PM ·
monodevelopbackgroundparallax
Is this the best way to do an AutoParallax Background ?
Hello everybody, I'm developping a game that require a Background that scroll in a infinite loop and so I did this:
public GameObject prefab;
public float width;
public bool alreadyDid;
public float speed;
public string name;
private Camera camera;
public float cameraWidth;
public float cameraPosition;
public float xLastFrameSprite;
public float xFirstFrameCamera;
// Use this for initialization
void Start ()
{
SpriteRenderer sprite = gameObject.GetComponent<SpriteRenderer> ();
width = sprite.bounds.size.x;
alreadyDid = false;
camera = Camera.main;
cameraPosition = camera.transform.position.x;
}
// Update is called once per frame
void FixedUpdate ()
{
cameraWidth = renderer.bounds.size.x;
transform.Translate (new Vector2 (-speed, 0) * Time.fixedDeltaTime);
if (transform.position.x < 0) {
if (!alreadyDid) {
Vector2 v = new Vector2 (transform.position.x + width - 0.1f, transform.position.y );
Object obj = Instantiate (prefab, v, Quaternion.identity);
obj.name = name;
alreadyDid = true;
}
}
xLastFrameSprite = gameObject.transform.position.x + (width / 2);
xFirstFrameCamera = cameraPosition - (cameraWidth / 2);
if (xLastFrameSprite < xFirstFrameCamera) {
Destroy (gameObject);
}
}
And it works very well but I wonder is this the best way or I can approach it in a different mode ?
Comment
Answer by cristian-chidean · Jan 27, 2014 at 08:27 AM
This is almost the same way i did it and it seems pretty efficient but when I added 4 layers of sprites with transparency, the performance drops signficantly. Is there a shader or some trick to add screen-size sprites with transparency?
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How can i shuffle a list 5 Answers
Is it possible to use DataContractJsonSerializer? 2 Answers
Unity App / Game to run in background, hidden 0 Answers
The Parallax goes way too fast 2 Answers