Search Unity

[Noob Question] Having a basic, unchanging background spawn all around player.

Discussion in '2D' started by UnitedNoob, Apr 30, 2017.

  1. UnitedNoob

    UnitedNoob

    Joined:
    Jan 2, 2017
    Posts:
    2
    I've looked everywhere for some way to have "infinite terrain". My background tiles are identical, and when I try to either move it when the player moves or make it a child to the player, the background seems to move and the player looks static. This is because it's constantly drawing the background where the player is.
    I came up with a little script that, when the player passes the middle point of the starting background, inefficiently instantiates tons of objects. It looks like a plus shape, but has no diagonal direction, which makes it unusable and incomplete. :(

    Is there some way to make the backgrounds spawn all around the player. without the ("static player") problem I described above?
    Here's the "cross" part of the script, where terrainGrowRate = 1 and iLeft, iRight, etc... are all initialized to 0:
    Code (CSharp):
    1.         if (!(CrossPlatformInputManager.GetAxis ("Horizontal") == 0 && CrossPlatformInputManager.GetAxis ("Vertical") == 0)){
    2.             //Left of middle
    3.             if(background.position.x/2 > transform.position.x) {
    4.                 Instantiate (background, background.position+new Vector3(terrainGrowRate*-1*iLeft,0f), transform.rotation);
    5.                 iLeft+=1f;
    6.  
    7.             }
    8.             //Right of middle
    9.             if(background.position.x/2 < transform.position.x) {
    10.                 Instantiate (background, background.position+new Vector3(terrainGrowRate*iRight,0f), transform.rotation);
    11.                 iRight+=1f;
    12.  
    13.             }
    14.             //Up of middle
    15.             if(background.position.y/2 < transform.position.y) {
    16.                 Instantiate (background, background.position+new Vector3(0f,terrainGrowRate*iUp), transform.rotation);
    17.                 iUp+=1f;
    18.             }
    19.             //Down of middle
    20.             if(background.position.y/2 > transform.position.y) {
    21.                 Instantiate (background, background.position+new Vector3(0f,terrainGrowRate*-1*iDown), transform.rotation);
    22.                 iDown+=1f;
    23.  
    24.             }
    25.  
    26.         }
    As you can see (from my non-existent scripting ability), I'm new to Unity. Can anyone please help out? Thanks! :)
     
    Last edited: Apr 30, 2017
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    maybe using texture offset ?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class tex : MonoBehaviour {
    6.  
    7.     Renderer rend;
    8.     public float speed;
    9.     Vector2 offset;
    10.  
    11.     void Start () {
    12.         rend = GetComponent<Renderer>();
    13.     }
    14.  
    15.     void Update () {
    16.         float x = Input.GetAxis ("Horizontal");
    17.         float y = Input.GetAxis ("Vertical");
    18.         offset = new Vector2 (offset.x += x * Time.deltaTime * speed, offset.y += y * Time.deltaTime * speed);
    19.         rend.material.SetTextureOffset("_MainTex", offset);
    20.     }
    21. }
    here example 5.5.3
     

    Attached Files: