A spritesheet-like texture has 8x4 individual frames on it. In a C# script I’m trying to pick one frame (31 in the following example) and, during run-time, alter the texture scale and offset to only show that selected frame. Take a look at my code:
const int framesX = 8;
const int framesY = 4;
const int frames = framesX * framesY;
int frame = 31; // for example
Material material = renderer.material;
material.mainTextureScale = new Vector2(1f / framesX, 1f / framesY);
material.mainTextureOffset = new Vector2((frame % framesX) * material.mainTextureScale.x,
(frame / framesX) * material.mainTextureScale.y);
Debug.Log("offset: " + material.mainTextureOffset.x + "/" + material.mainTextureOffset.y);
8x4 = 32 frames total on the texture, we’re going for frame 31, ie. the last frame. The debug output shows correct values of 0.875/0.75 which are 7/8 and 3/4 accordingly on the 8x4 sheet. But for some reason I’m not seeing the last frame in-game, but the one above (from the looks of it atleast) instead. The scaling works properly. Any ideas?