I’m brand new to Unity (literally a few days), and coding in general, so I’ve only just made a simple Flappy Bird clone.
I wanted my sprite to point 45 degrees up or down depending on the GameObject’s y velocity, but rather than making a 45 degree rotated sprite, I’ve decided to try and figure out the rotation transformations.
After lots of trial and error, I thought I finally had it working, but when finally running the compiled game in full screen, I’ve noticed a persitent jittering when the y velocity was negative. The code relevant code running in Update is as follows:
//give a y velocity up on mouse click
if (UnityEngine.Input.GetMouseButtonDown(0) == true && CakeAlive == true)
{
Cupcakephysics.velocity = Vector2.up * flapStrength;
//This refers to the Rigidbody2D Component of the object with Gravity 1
}
//a float to keep the angle value for comparison
zRotation = transform.rotation.eulerAngles.z;
Debug.Log("Beggining");
Debug.Log(zRotation);
//rotate up if velocity is up and between -45 and 45 degrees from horizontal
if (Cupcakephysics.velocity.y > 0 && ((zRotation >= 315 && zRotation <= 360) || (zRotation <= 45 && zRotation >= 0)))
{
transform.eulerAngles = transform.eulerAngles + new Vector3(0, 0, 45) * Time.deltaTime * rotationSpeed;
}
//hard set angle to 45 if code overshoots
else if (Cupcakephysics.velocity.y > 0 && zRotation > 45 && zRotation <= 180)
{
transform.eulerAngles = new Vector3(0, 0, 45);
}
//rotate down if velocity is down and between -45 to 45 from horiontal
else if (Cupcakephysics.velocity.y < 0 && ((zRotation >= 315 && zRotation <= 360) || (zRotation <= 45 && zRotation >= 0)))
{
transform.eulerAngles = transform.eulerAngles + new Vector3(0, 0, -45) * Time.deltaTime * rotationSpeed;
}
//hard set to -45 if code overshoots
else if (Cupcakephysics.velocity.y < 0 && zRotation < 315 && zRotation >= 180)
{
transform.eulerAngles = new Vector3(0, 0, -45);
}
//just to reset if something goes sideways
else
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
Debug.Log("End");
Debug.Log(zRotation);
(Rotation speed is set to 5)
As you can see, I have the two logs running to check the angle before and after the code runs, just to see what happens, and if there’s any difference because there might be some other issue elsewhere in the code. It seems like ther MIGHT be a problem elswhere, because the log looks like this
With Beggining and End being the same, but the next Beggining being different. But if there IS a problem eswhere, I don’t know where to check.
When the button is mashed and the y velocity is positive, up, the Object behaves exactly as intended, rotating smoothly from whatever angle it was at until 45, then sitting EXACTLY at 45, the Debug Logs confirm that, both before and after. Straight as an arrow.
But when the gravity physics takes over and y velocity becomes negative, it’s suddenly unstable. It still rotates smoothly, but no longer stays dead straight at 315, instead very rapidly oscilating between 315 and 313, seen in the first log. This causes a very noticeable jittering effect. But this occurs ONLY as the object is continously falling under gravity.
I’m at a loss why, the rotation doesn’t depend on anything but the sign of the velocity, so why is it not the same going up.