- Home /
Moving A Sprite Then Returning It To Position?
How can I move a sprite/texture left for a couple of seconds or to a certain position, then return it back to it's original position(where it starts)? The reason I want to do this is because my background scrolls to the left, but when it scrolls off screen the scene background becomes blue and bland because the clouds have scrolled off screen. My texture that I have consists of clouds and a blue background, but it only looks like the clouds move so again they move off screen and reset but they take a while and I don't want to increase the speed of it because then it will look too fast.
So, my texture scrolls but doesn't actually move. I want something that will actually move my textures/sprites. If your curious as to how my background scrolls, here's my script:
public class BackgroundScroller : MonoBehaviour {
public float speed = 0;
public static BackgroundScroller current;
float pos = 0;
void Start(){
current = this;
}
void Update(){
pos += speed;
if(pos > 1.0f)
pos -= 1.0f;
renderer.material.mainTextureOffset = new Vector2(pos, 0);
}
}
Answer by haim96 · Aug 19, 2014 at 08:28 PM
Good tutorial about scrolling background.
http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/2d-scrolling-backgrounds
there are several method there so you can pick the one that fits you.
Well I tried the first method in the video, and I can't get it to work. It won't scroll my background or anything. That method out of the 3 is the best for me and what I'm looking for, but it's just not working for me. Isn't there a way that I can change my script that I posted to make it scroll like the first method in the link?
Answer by smoggach · Aug 19, 2014 at 08:22 PM
Change the position or localPosition of the sprite's transform.
Sorry but how and why? I just need a better understanding of it. I hope you can understand me and where I'm co$$anonymous$$g from.
transform.position transform.localPosition
transform is your gameObject's position, rotation, and scale in the world.