# If (transform.position.. is immediately snapping to position

I’m trying to create a continuously scrolling background in a runner sort of 2d game where the actual player model is only moving along the x axis and everything is moving past the player (creating the illusion the player is moving).

However, I am having trouble getting the background to repeat itself by snapping back to a new position. The transform.position.y of the game object I’m attaching this script to is at 0, but no matter what I make the requirement in the if statement my object immediately locks to the (x,15,z) coordinate. No scrolling is achieved (although it will easily scroll along the x axis in the float). Thanks.

``````using UnityEngine;
using System.Collections;

public class BackgroundScroll : MonoBehaviour
{
public Vector2 velocity = new Vector2(0, 15);

void Update()
{

if (transform.position.y > 84f);
{
transform.position = new Vector3(transform.position.x, 15f, transform.position.z);
}
rigidbody2D.velocity = velocity;

}
}
``````

Well, I haven’t worked with rigidbodies much, but, if this is applied to a background object, why is there a rigidbody attached?
You could get rid of the rigidbody and just use

`````` transform.Translate()
``````

Also, you might want to change that 84f to 84.0f, you put the f after numbers with a decimal point in them (floats), not whole integers.
Here is what I suggest doing:

``````using UnityEngine;
using System.Collections;

public class BackgroundScroll : MonoBehaviour
{
public float yspeed = 2.0f;
public float maxdistance = 84.0f;
float startingposition;

void Start()
{
startingposition = transform.position.y;
}

void Update()
{

if (transform.position.y > startingposition + maxdistance)
{
transform.position = new Vector3(transform.position.x, startingposition, transform.position.z);
}
else
{
transform.Translate(new Vector3(0.0f, yspeed * Time.deltaTime, 0.0f));
}
}
}
``````

the yspeed * Time.deltaTime will cause the object to move along the y axis at yspeed units per second.
Also, you could move this object anywhere, and it will still only travel as far as maxdistance, and will reset itself to its starting position.

EDIT: Oops, I only made y axis movement because the only thing you are checking is the y position…
x and z axis movement wouldn’t be to hard to add though.

Look at this video about background scrolling from unity live training http://www.youtube.com/watch?v=GtNW9uD94uc