- Home /
How to create an infinite scrolling background in top down multi-directional shooters.
I am creating an Asteroids-esque game. I'd like to create an infinitely scrolling background for top down shooters. I've tried other several scripts but it wasn't helping me.
Other videos only provides tutorials for one direction background parallax scrolling.
I was thinking if there are some sample scripts I could base my code on.
Answer by akruckus · Sep 11, 2014 at 09:18 PM
What if you were to just create a one direction scrolling background but rotate the background object based on the direction and speed of the player? You'd have to toy around a bit because you wouldn't want any stars/planets on screen to rotate with the player object.
Indeed, I've tried using some methods such as duplicating the image all over the infinite plane, but I think that's gonna be an issue in performance.
Answer by Malfegor · Sep 12, 2014 at 07:48 AM
You could try make the Parallax scroll in 2 dimensions. Try the following (note: untested!)
public static class Global : MonoBehaviour
{
public static Vector2 WorldMove = Vector2.zero;
}
This is a class, which has to be in the scene you want to scroll. Now for the player:
public float MoveSpeed = 5F;
public class PlayerController : MonoBehaviour
{
void Update()
{
Global.WorldMove = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")) * MoveSpeed;
}
}
This is (obviously) the player controller script. Assign this script to a player.
Now for the 'Layers' (parallax)
public class Layer : MonoBehaviour
{
private Vector2 MaxDistance;
private Vector2 StartPosition;
void Start()
{
transform.position = Vector3.zero;
StartPosition = (Vector2)transform.position;
MaxDistance = Vector2.zero;
for(int i = 0; i < transform.childCount; ++i)
{
MaxDistance += new Vector2(transform.GetChild(i).renderer.bounds.size.x, transform.GetChild(i).renderer.bounds.size.y);
}
MaxDistance = MaxDistance / 2;
}
public void Update()
{
transform.Translate(Global.WorldMove * Time.deltaTime);
if(transform.position.y <= -MaxDistance.y || transform.position.y >= MaxDistance.y || transform.position.x <= -MaxDistance.x || transform.position.x >= MaxDistance.x)
{
transform.position = StartPosition;
}
}
}
The Update function might not work as properly as it should, so say so if it doesn't! Now, plant this 'Layer' class on an empty GameObject and put 4 sprites/planes for your floor in. Position them so that they make one big square. Now it should work, but as stated earlier: Untested! Hope it helps (even if only a tad).
Your answer
Follow this Question
Related Questions
2D Parallax Background 4 Answers
Moving Background with translated images 1 Answer
Making a point and click background. 0 Answers
trouble with getting the background to show past the midground in 2d parallax backgrounds 0 Answers
Problem with changing the direction of an Infinite scrolling background. 1 Answer