Thursday, 26 March 2015

Unity 5: Infinitely Omni Directional Scrolling Background

2 Hours Research


    After much research, trial and error, I have found the code that allows for this. For some reason SetTextureOffset will not work, nor is any other method I tried efficient enough to be effective.

    For this method you need a background with a seamless texture that covers the whole camera view, then a controller that allows for 2d movement. Drop the Player GameObject onto the public GameObject and the backgrounds material onto the Material section. You need to include the /100, realistically anything greater then /10 will work. Each tile is worth 1 unity unit. So if your player GameObject were to move 1 unit the background would go to a new tile.


    public Material material;
    public GameObject player;


    void Start()
    {
        material = renderer.material;
    }


    void FixedUpdate()
     {
         renderer.material.mainTextureOffset = new Vector2(player.transform.position.x / 100, player.transform.position.y / 100);
     }

No comments:

Post a Comment