- Home /
How would I make my object fall onto a specific direction
Hello, I am a newbie in unity. I have created a object which has a Rigidbody component and it fall towards the -y axis. Also i have added a score game object which tells the score of the player everytime it falls but as the object is falling towards -y axis it tells score too in minus which looks pretty bad I have written following block of code to update score everytime it falls:
Score.text = Player.transform.position.y.ToString("0");
When it executes it gives output like; -7,-8-9 etc. I just want that the same (-7,-8,-9) to be shown in +.
If you use $$anonymous$$athf.Abs, to get the absolute value of a float, it always returns the positive value. For example:
Score.text = $$anonymous$$athf.Abs(Player.transform.position.y).ToString("0");
What does the "0" parameter in the ToString $$anonymous$$ethod do?
I believe that keeps it a whole number, by truncating (chopping off) the decimal point digits instead of rounding them.
You can also do float output=Player.transform.position.y; if(output<0) output=-output; because the negative of a negative is positive. Or you could convert it to a unsigned variable, unsigned means exactly that, no signs, no + or negative values, just values.
Answer by Daviiid · Feb 12, 2021 at 11:16 PM
Score.text = (Player.transform.position.y * -1).ToString("0");
Your answer
Follow this Question
Related Questions
cloning bullet object 1 Answer
rigidbody 2d addfoce by vector math 0 Answers
Having problems with rigidbody gravity 1 Answer