- Home /
Is it possible to add a point to the score, everytime the Player rotates 180°?
Hey there, I'm relatively new to unity and programming itself, so bare with me on this one :D I wondered if it is possible to add a point to the score, everytime the player turns 180°. I'm trying to program a "Trampoline"-game, where everytime you do a front flip, a point gets added.
Thank you (it advance) for helping :)
Answer by Nomenokes · May 10, 2018 at 12:25 PM
How about something like this, where every time it passes both upright and upside-down positions, a point is scored?
int score = 0;
float prevX; //the "x" rotation from last tick
float prevZ; //the "z" rotation from last tick
bool justFlipped = false; //if it was last turned upside down, this will be true, and if it was last upright, it will be false
const float ANGLE_BUFFER = 5; //how lenient the "flips" are
void Update(){
if(prevX>180) prevX = 360-prevX; //turns "340 deg" into "-20 deg"
if(prevZ>180) prevZ = 360-prevZ; //for easier calculations
float currentX = transform.eulerAngles.x;
float currentZ = transform.eulerAngles.z;
if(currentX>180) currentX = 360-currentX;
if(currentZ>180) currentZ = 360-currentZ;
if(
justFlipped = true &&
(
(Mathf.Abs(prevX) < ANGLE_BUFFER /*if x angle is close enough to 0*/
&& Mathf.Sign(prevZ) != Mathf.Sign(currentZ)) /*and Z has flipped*/
|| /*or*/
(Mathf.Abs(prevZ) < ANGLE_BUFFER /*if z angle is close enough to 0*/
&& Mathf.Sign(prevX) != Mathf.Sign(currentX)) /*and X has flipped*/
)
){ //this means that the player has just flipped upright
justFlipped = false;
score ++;
}
if(
180 - (Mathf.Abs(prevX) < ANGLE_BUFFER /*if x angle is close enough to 180*/
&& Mathf.Sign(prevZ) != Mathf.Sign(currentZ)) /*and Z has flipped*/
|| /*or*/
180 - (Mathf.Abs(prevZ) < ANGLE_BUFFER /*if z angle is close enough to 180*/
&& Mathf.Sign(prevX) != Mathf.Sign(currentX)) /*and X has flipped*/
){ //this means the player has just flipped upside down
justFlipped = true;
}
prevX = transform.eulerAngles.x;
prevZ = transform.eulerAngles.z;
}
I haven't tested it out, so it might not work, so let me know. I also realize that the code isn't perfectly streamlined, because I wanted to make it easier to understand rather than perfectly efficient.
Cheers,
Nomenokes
Answer by JFox23 · May 10, 2018 at 04:41 PM
First of all, thank you VERY much. Everything seems to be ok, except
if (
180 - (Mathf.Abs(prevX) < ANGLE_BUFFER /*if x angle is close enough to 180*/
&& Mathf.Sign(prevZ) != Mathf.Sign(currentZ)) /*and Z has flipped*/
|| /*or*/
180 - (Mathf.Abs(prevZ) < ANGLE_BUFFER /*if z angle is close enough to 180*/
&& Mathf.Sign(prevX) != Mathf.Sign(currentX)) /*and X has flipped*/
)
Here, it tells me the error-code "CS0019": "The -- operator can't be used on "int" and "bool" (translated from german, sorry if there are mistakes^^).
Again, thank you for your help. Maybe you know, what the problem is?
$$anonymous$$y bad, I messed up a parentheses
if (
(180 - $$anonymous$$athf.Abs(prevX) < ANGLE_BUFFER /*if x angle is close enough to 180*/
&& $$anonymous$$athf.Sign(prevZ) != $$anonymous$$athf.Sign(currentZ)) /*and Z has flipped*/
|| /*or*/
(180 - $$anonymous$$athf.Abs(prevZ) < ANGLE_BUFFER /*if z angle is close enough to 180*/
&& $$anonymous$$athf.Sign(prevX) != $$anonymous$$athf.Sign(currentX)) /*and X has flipped*/
)
And the same parentheses change for the other "if" later on, just move it from in front of $$anonymous$$athf to in front of the 180.
Do you get what this code does, even if you don't get the exact syntax?
Hm ok, now Unity is telling me:"warning CS0414: The private field `Player.justFlipped' is assigned but its value is never used"
Is this a problem? I think so, however I'm not sure lol
Also, if it is ok to ask, how would I then display the score? Your score-code is now placed in my player class, so is there a way to use it from there to show it in some kind of UI? Or do I need a new script for it?
Again, thanks for helping :)
Ok, this is just another syntax mistake. It should be justFlipped == true
not justFlipped = true
. If you make a reference to some UI text on the screen (add this with the editor first), you can do something like this:
public Text scoreText; //assign this in the editor
void Update(){
//your other stuff
scoreText.text = score.ToString();
}
where the UI text has its value changed every tick to the player's score
I completely get it, thanks a lot! Still would've never figured it out without your help though :D I'm going to try it later today, will get back to you :)
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Object rotates more farther it is away from a point [C#] 0 Answers
Point system, something wrong 1 Answer
Distribute terrain in zones 3 Answers