- Home /
Question by
Pranav47 · Jan 18, 2019 at 08:57 PM ·
scripting problemif-statementsscore systemgyroscopeupdate function
How to increment score by one, every time player moves the device face down ?
I am facing an bug, that the score is increasing very fast rather than once per downWard rotation.
My code is like this:
private int score; //Score of player
private void Start()
{
score = 0; //Initialise the score
}
private void Update()
{
if (Input.deviceOrientation == DeviceOrientation.FaceDown)
++score;
}
Please help me to make it work as expected.
Comment
Answer by Hellium · Jan 18, 2019 at 09:05 PM
private int score;
private DeviceOrientation lastDeviceOrientation;
private void Start()
{
score = 0;
}
private void Update()
{
if (Input.deviceOrientation == DeviceOrientation.FaceDown && lastDeviceOrientation != DeviceOrientation.FaceDown )
++score;
lastDeviceOrientation = Input.deviceOrientation ;
}
Answer by xxmariofer · Jan 18, 2019 at 09:12 PM
You can change the code to this
DeviceOrientation orientation;
private void Awake()
{
orientation = DeviceOrientation.Unknown;
}
void Update()
{
if(orientation != Input.deviceOrientation)
{
if(Input.deviceOrientation == DeviceOrientation.FaceDown)
{
score++;
}
orientation = Input.deviceOrientation;
}
}
couldnt test it
Your answer
Follow this Question
Related Questions
Update within an if statement not working? 3 Answers
Multiple coroutines starting from Update() freeze/crash Unity 0 Answers
Score counter breaking after adding points 2 Answers
Playing the right animation when running and not running 0 Answers
I want my score to reset back to 0 but keep my highscore saved 3 Answers