- Home /
Simple Maths Problem (I Suck At Maths)
Okay,
I have a variable called EnemyRemaining, which equals 30 to begin with, but an enemy is destroyed every 5 seconds (this bit isn't relevant)
So, my variable is decreasing from 30, to 29, to 28 ....
But I would like to display this as an overall percentage. When I have 30 Enemies Remaining - the overall percentage is 0% complete, then when all the enemies die off to 0 Enemies Remaining, the overall percentage is 100% complete (15 enemies = 50% ...)
Could someone help me with the equation and script.
Thanks
Answer by oliver-jones · Nov 18, 2010 at 03:56 AM
Solved it my self:
static var EnemyRemaining : float = 30; // Decreased every time enemy is killed static var EnemyMax : float = 30; // Total amount of enemy to begin with static var EnemyAdding : float = 0; //IMPORTANT BIT - Increases every time enemy is killed - from 0 to 30 -- this allows my percentage to read the other way round - from 100% -0% to 0% - 100%
var FindOne : float; // This gets EnemyAdding(remaining) and divides it by EnemyMax(total); static var FindTotal : int; // This then multiplies it my 100 to make it a %.
function Update() { FindOne = EnemyAdding / EnemyMax; FindTotal = FindOne * 100; print(FindTotal); }
If you want the percentage to read the opposite way round, then I replace EnemyAdding with EnemyRemaining.
The Destroy Enemy has:
EnemyRemaining --;
EnemyAdding ++;
Your answer
Follow this Question
Related Questions
Calculating percentage in code is reversed? 1 Answer
Progression height affected by the width ? why 1 Answer
How can I dynamically change the percentage value so that it always adds up to 100%? 1 Answer
How to move slider same amount with different max values when moving it by a percentage Unity 1 Answer
Math - distance to percentage 2 Answers