# C# Looking for help with rotating sprites

## Recommended Posts

I am trying to rotate my car sprite just slightly to the left or right depending on input up to a small maximum. When I use this code, the rotation doesn't stop at the maximum i give it. When i try to reset the rotation back to its original state when not pressing any input, the car also jitters between a z axis rotation of 0 and 2.5. I don't understand why the rotation doesn't stop at the given maximums, nor do I understand why its jittering. Can anybody provide me some insight?

float movementHorizontal = 0f;
float movementVertical = 0f;

if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D))
{
}
else if (Input.GetKey(KeyCode.A))
{
movementHorizontal = -1;

if (transform.rotation.z < 10f)
{
transform.Rotate(zAxis, 2.5f);
}
}
else if (Input.GetKey(KeyCode.D))
{
movementHorizontal = 1;

if (transform.rotation.z > -10f)
{
transform.Rotate(zAxis, -2.5f);
}

}
else
{
if(transform.rotation.z > 0)
{
transform.Rotate(zAxis, -2.5f);
//transform.rotation.Set(0, 0, 0, 0);
}
else if (transform.rotation.z < 0)
{
transform.Rotate(zAxis, 2.5f);
//transform.rotation.Set(0, 0, 0, 0);
}

}

##### Share on other sites

Floating point numbers have a finite precision. In your last else branch you check for less than 0 and greater than 0, and doing nothing if equal to zero. However, the probability that the value is exactly 0 after some computation like rotation is very low; it is much more likely that values like 0.0000000123 will result. This is often the reason for stability problems like you observe.

One approach to overcome that problem is to weaken the comparison by considering a small tolerance value, i.e. check not for equal to 0 but very close to 0, like so:

if(transform.rotation.z > 0.01)
...
else if(transform.rotation.z < -0.01)
...
else
...

Finding an appropriate tolerance is a problem though, because it depends on the circumstances. In your case we're speaking of an orientation angle in degrees (presumably), so 0.01 is probably already a small enough value.

Another thing is that restricting values (to a maximum in your case) is sometimes better be done on the result, i.e. after the computation. This is a way to avoid overshooting.

##### Share on other sites

Wow, thanks to you both! I looked closer and my rotation was indeed not quite 0. The Mathf.Approximately was unknown to me. Thanks for pointing it out, I will give it a try. If it doesn't work out, I will try a manual check. Thanks again, fellas!

##### Share on other sites

Nice , it is very good topic , but there are very few comments

## Create an account

Register a new account

• 10
• 17
• 9
• 13
• 41
• ### Similar Content

• Hey There,
I am a developer and Im working on a blockchain based infinite runner type game. Right now, I am working on releasing the beta version with a couple other game developers, but would love to expand the team and have other talented and bright people contributing. The game portion of the project isnt very complicated, and wouldnt require anyone to pull thier hair out for it.
Thank you!

• Hello,
I want to get into coding again by programming a 2D platformer to get started, but i don't know if i should use Java or C# with the unity engine.
I am pretty fit with Java, but with c# i have to start from scratch. What do you recommend and why?

• Ok, firstly, Hi.

This is my first post on this forum. I am an Indie Dev making my first game so bear with me when I say dumb stuff, I'm on a huge learning curve.

My first question is about inventory systems for unity. I am trying to make a survival type game with crafting. I have purchased Inventory manager pro by devdog from the unity asset store and it seems like a pretty powerful assett but for an intermediate coder its a little tough to use.  I'm beginning to wonder if it was the right purchase.
So my question is.... does anyone have any experience of inventory plugins / systems for unity and can anyone reccomend a system to me?
It needs to have the following: Loot system, crafting system, character sheet, blueprint system,  character stats system. Ideally with as little coding as possible.

Thanks

• I've got a bug with my brick breaker style game. The bricks move down one line at a time ever 1.5 seconds. What appears to be happening is occasionally the ball will be just about to hit the brick when the brick moves down a line, and now the ball is behind it. I'm not sure how to fix this. I have two ideas but I'm not sure of implementation. 1 solution would be to check where they were and where they are going to be before rendering the frame. Then if they crossed paths, then register the brick as hit. Solution 2 would be change how the bricks move. I could maybe slide them down line by line, instead of a jump down. I'm not sure of this will fix the issue or not. Any ideas?

• Once again Unity is frustrating me to the point of insanity.
What I am looking for is a way to find a ray intersect with the edges of the mesh, using Unity's already made collision system. I want to point out that I know how to do a line intersect, what I want to know is if Unity supports this already.

The image above shows how I sweep a ray,intersecting the mesh. The top green image shows what I want and the red shows what Unity is giving me.
I want to know if there is some way, to find the edges in Unity without creating a custom line intersection tool.
Most engines I know don't use rays for this but instead use a plane like this:

I checked the Unity "Plane intersection" but it is just a ray cast. It will still need me to find the vertices on the collision mesh to cast the ray from; if I am doing that then making my own line intersection tool is better.

I looked online and can find anything on this. Also I don't want to cut the mesh, so I don't need a way to know what side is what.
Does Unity even have collisions that support edge only detection?