- Home /
Raycast // Accuracy math pls help
Sooo, Im trying to figure out the coding to do Accuracy math, currently doing the number of targets hit / the amount of times youve fired in total, then taking that number and multiplying by 100. For some reason my text keeps showing either 100% acc or 0% This is the code
Answer by unity_ek98vnTRplGj8Q · Jun 09, 2021 at 08:13 PM
All of your variables are integers, meaning they always have to be a whole number. Your accuracy should be a float, and your other values need to be cast as a float as you are doing math.
float accuracy;
int shotsFired, shotsHit;
accuracy = ((float)shotsHit / (float)shotsFired) * 100f;
Awesome! Thanks so much, so im now running into an issue where it will only show the percentage as long as there's no repeating decimal. Is there a way to force the code to round to the nearest tenth? Or whole number even?
You can use a string format to just get the number to display to the tenth's place
acc.text = "Accuracy: " + string.Format("{0:0.#}", acurracy) + "%";
You can change the formatting to show as many or as few decimal places as you want
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
What's wrong with my RaycastHit2D? 1 Answer