Barry isn't going back?

Hello, I want barry to go back when I press “keypad0” in Snowboarder game and I wrote the following code
if(Input.GetKey(KeyCode.Keypad0)){
player.transform.rotate(0,180,0);
backturn=true;
} else if (backturn==true && Input.GetKey(KeyCode.Keypad0)){
player.transform.rotate(0,0,0)
backturn=false
} The problem is that every time a key is pressed, sometimes the y coordinate becomes -180. I tried Mth.Abs, but sometimes it takes values like 179,0001 or 180,0001 and it doesn’t work as stable as I want. Does anyone have any suggestions for bary to go back stable?

Hi,

Welcome to our community! :slight_smile:

What do you mean by “back”? Could you please elaborate on your idea a bit futher?


See also:

x axis right, left y axis up and down.
barry normally goes to the right on the x-axis. I want it to go to the left on the x-axis.

Have you already tried to use the velocity of the Rigidbody2D component?

The rotation does not have anything to do with the direction in which Barry is moving. Unless I’m misunderstanding your idea.

I multiply its speed by -1 to make it go backwards. Not as a speed, but as a barry character, it does not return every time you press the “0” key to the left.
I can’t always say barry.transform.rotate and turn it 180 degrees.
When you press the “0” key a few times, it’s 179.9999 degrees or like 180.00001 so barry is looking to the right again. My problem is that when I press the “0” key, barry rotates 180 degrees. it happens but sometimes it doesn’t return because it is 179,999. Sorry for my bad english.

The problem is very likely that the rotation consists of floats. Unless you assign a hard-coded value, you’ll almost never get perfectly rounded values.

If Barry needs a rotation of exactly 180f, you could test transform.rotation. Take a look at the example. You need Quaternion.Euler because Unity internally works with quaternions, not Euler angles.

Privacy & Terms