- Home /
how to make a texture scroll on an object?
I'm trying to make a map for mobile game where the user scrolls on the map of the planet in a loop, so if u scroll to the left edge you will start seeing the right edge and so on. it works fine by rotating the object when input key is pressed but i want to rotate the texture on the object not the object itself. or any other solution. any help would be appreciated.
In a word - simply change the offset. Just try it in the editor so you get the idea.
Answer by digitalConundrum · Jan 20, 2012 at 06:20 PM
Meltdown's answer would work, though you could also animate the texture scrolling by changing the tiling to a lower amount (perhaps x=.5,y=.5) and then increment or decrement the offset values to cause the texture to scroll without having to move the actual object.
here is a sample script I wrote in an old project to create a pulsating beam by scrolling the texture on a linerenderer based on time and a constant value. You could adapt this approach to your project by changing it to rely on time.deltaTime*input instead of a constant value.
public class beamScroll : MonoBehaviour {
LineRenderer thisRenderer;
// Use this for initialization
void Start () {
thisRenderer = gameObject.GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update () {
thisRenderer.material.mainTextureOffset = new Vector2(thisRenderer.renderer.material.mainTextureOffset.x + .25f * Time.deltaTime,
thisRenderer.renderer.material.mainTextureOffset.y + .25f * Time.deltaTime);
}
}
Hope this helps!
Answer by Meltdown · Jan 20, 2012 at 06:09 PM
Very easy.
Create one really long plane. Apply your texture to it, then on the material settings tile the texture on the plane so it repeats many times. Then when your user scrolls this plane, the texture scrolls.
Do not worry about having a plane that is say 10000 units long and is tiled, very little performance impact.
Also make sure the texture's import settings support repeating.
Answer by chronos · Jan 20, 2012 at 11:06 PM
OK thanks a lot for the help i got it to work using void Update () { float direction = Input.GetAxis("Horizontal"); if (direction != 0) {
Vector2 offset = renderer.material.GetTextureOffset("_MainTex");
float xOffset = offset.x;
renderer.material.SetTextureOffset("_MainTex", new Vector2(xOffset + direction * scrollSpeed ,0));
}
but my problem now is there is another circle object on top of the plane, and when i scroll the texture on the plane I want the circle to stay in the relative position. I tried to update the position of the circle by the offset of the plane but these two variables are not linearly correlated, so the circle does not update correctly. any ideas?
$$anonymous$$ake the plane and your circle game object seperate game objects. What you do on the plane should do nothing to the circle object.
I think what the OP means is that he wants the circle to stay in the same position relative to the texture scrolling, not in world-space (Like a YOU ARE HERE marker.) That's gonna take a little bit of math(and/or trial and error) You'll have to convert the world space to the 2d coordinates of the texture on the plane and then use the current offset to move your circle.