- Home /
Infinite scrolling background
I am trying to make a game with the same scrolling background like Subway Surfers. How would you be able to do this with Unity? I am just starting out so I do not have much information, but from what I can see on other forum posts is that you would need a shader. I am 13 years old and this is the first game I am programming with Unity, so code examples are very very helpful. Thank you.
Answer by erick_weil · Feb 17, 2014 at 01:35 AM
try to adapt this code to your neededs, create a new scene, add a cube, set the cube scale.x to 2 and scale.z to 2 too put the script on the camera, asign the object variable with your created cube and have fun!
#pragma strict
var objectprefab : GameObject;
var whatIhavehere : GameObject[];
var objectscalez : float = 2;
var objectscalex : float = 2;
var numberofobjects : int;
var zoffset : float = 2;
var yoffset : float = -2;
var z : int;
var proxblock : int;
var centerx : float;
function Start () {
numberofobjects = 5;
whatIhavehere = new GameObject[numberofobjects*10];
transform.position.z += 3*Time.deltaTime;
z =Mathf.FloorToInt(transform.position.z);
proxblock = z+numberofobjects;
centerx = transform.position.x;
}
function Update ()
{
// transform a float position in a int position,
z =Mathf.FloorToInt(transform.position.z);
if(z+(numberofobjects) == proxblock)
{
var posi : Vector3;
posi = Vector3( centerx,transform.position.y+yoffset,z+(numberofobjects*objectscalez)+zoffset);
whatIhavehere[foundone()] =Instantiate(objectprefab,posi,objectprefab.transform.rotation);
whatIhavehere[foundone()] =Instantiate(objectprefab,posi-Vector3(objectscalex,0,0),objectprefab.transform.rotation);
whatIhavehere[foundone()] =Instantiate(objectprefab,posi+Vector3(objectscalex,0,0),objectprefab.transform.rotation);
proxblock += objectscalez;
}
if(Input.GetKeyDown ("a")&& transform.position.x >= centerx )
{
moveTo(transform.position.x - 1.5, 20); //Feed the moveTo() function the X/Y positions you want it to move to, and the Speed you want it to move at
}
if(Input.GetKeyDown ("d")&& transform.position.x <= centerx )
{
moveTo(transform.position.x + 1.5, 20); //Feed the moveTo() function the X/Y positions you want it to move to, and the Speed you want it to move at
}
transform.position.z += 3.0*Time.deltaTime;
}
function moveTo(posX : float, speed : float)
{
while(posX!= transform.position.x)
{
transform.position = Vector3.MoveTowards (transform.position,new Vector3(posX,transform.position.y,transform.position.z),speed * Time.deltaTime);
yield;
}
}
function foundone ()
{
for(var i=0;i<whatIhavehere.Length;i++)
{
if(whatIhavehere[i] == null)
return(i);
else
{
if((whatIhavehere[i].transform.position.z -z)<z-proxblock)
{
Destroy(whatIhavehere[i]);
return(i);
}
}
}
return(i);
}
Your answer
Follow this Question
Related Questions
GrabPass and transparent Queue materials 0 Answers
Best way to achieve a vector graphics effect? 2 Answers
Doom Classic Effect - Realtime Pixelated Shadows 1 Answer
shader problem 0 Answers
Unity 5 and PhysX shaders. 0 Answers