I am scratching my head over a very weird problem. It’s very straighforward. All I am doing is changing the red channel of the color of a pixel of a Texture2d a set amount x * Time.deltaTime in the Update function. If x is 0.1 or bigger it works perfectly fine but if it is smaller it doesn’t update the color channel at all. Is there a bottom limit for how small the increments can be or maybe something else? Anybody have an idea?
Let me give some more context because it might possibly be a bit more complex.
public class PlayerTracker : MonoBehaviour
{
public GameObject player;
public Texture2D playerMap;
public RawImage mapImage;
// Start is called before the first frame update
void Start()
{
playerMap = new Texture2D(20, 20);
for (int i = 0; i < 20; i++)
for (int j = 0; j < 20; j++)
playerMap.SetPixel(i, j, Color.black);
playerMap.Apply();
mapImage.texture = playerMap;
}
// Update is called once per frame
void Update()
{
PositionTracker();
}
public void PositionTracker()
{
int xToTexCor = Mathf.FloorToInt(player.transform.position.x / 4.5f);
int zToTexCor = Mathf.FloorToInt(player.transform.position.z / 4.5f);
Color currentColor = playerMap.GetPixel(xToTexCor, zToTexCor);
currentColor.r += 0.2f * Time.deltaTime;
playerMap.SetPixel(xToTexCor, zToTexCor, currentColor);
playerMap.Apply();
}
}
I am taking the players position and translating it into a texture. The longer they stay in a position the redder the corresponding pixel gets. Logging the the red channel shows that it only updates every few seconds if the rate of change is smaller than 0.2f * Time.deltaTime and the change is not as big as it should be.
So weirdly enough when taking an intermediary variable to hold the change in color I could see that the red channel was updating at the correct rate but not every frame.
(For some reason it didn’t let me upload an image so here is a link for a consol log showing the discrepancy)
imgur image