Offset texture based on world space

I’m looking for a way to make an object’s texture offset when I move it in the scene. Let’s say I have a cube, a 1024x1024 texture atlas divided in 4 equal parts with 4 different colors (red, green, blue, and yellow; just as an example), and the cube’s UV will cover one of those 4 sides, so it will show one of the four colors. How do I make the texture continuously offset while I move the cube in my scene, so that everytime I move it, it will show one of the colors on the texture atlas? And if I keep moving it, it will keep scrolling through the four colors, over and over again.

I was thinking of having the texture offset half way in a direction so it will show one of the other colors, but I don’t know how to make that happen while the cube’s position changes in my scene. Can someone point me in the right direction?

Thank you.

If you’re using the built in Standard Shader (I’m sure some of the other shaders do this too, but I’m not in front of Unity and can’t check) you can change the Offset property to move the texture.

Basically,

  1. Store your current position as the last position you have ‘stepped’ the texture offset at
  2. Every update, check to see if you’ve moved more than some threshold, in x and y each. If you have:
  3. Update the objects material property Offset x or y (corresponding to which direction you moved in) to equal its current value + .5 (the distance across half the texture in UV space).

Hope this helps!