- Home /
Setting GUI color alpha math issue.
I'm pretty sure my math is just broken but Im basically trying to set the alpha of something relative to the player's Y position.
Im using this math to set the alpha:
if (PlayerConditions.playerY < 290){
GUI.color.a = 1 - ((PlayerConditions.playerY - 170) / 120);
Debug.Log(1 - ((PlayerConditions.playerY - 170) / 120));
}
else{
GUI.color.a = 0;
}
The debug log is returning 1 which means the object has 100% opacity which is obviously not what I want.
What's wrong with my math here?
In case you're wondering 170 is the height of my death floor which kills the player. 290 is where I want the object to start becoming visible.
Answer by Bunny83 · Dec 09, 2013 at 04:31 AM
GUI.color.a = 1.0 - ((PlayerConditions.playerY - 170.0) / 120.0);
You used integer values. When you divide by 120 the value becomes 0 due to integer division (1 - 0 == 1)
Answer by paulygons · Dec 09, 2013 at 04:32 AM
I spent the past 10 minutes working through this and your math is right (my math is weak and feeble). I think the problem is that you are working with integers and not floats. Try changing the 1 to 1.0 or putting an "f" after it.
AH, bunny beat me.