(I think I fixed my issues on my own but if somebody could chime in with advice, I’d appreciate it.)
I had an issue with a choppy physics2D framerate (running via FixedUpdate) on a specific rigidbody2D (affecting only the rotation), and I was able to fix it by increase the rate of Fixed Timestep from (iirc) 0.02 to 0.005. It seems increasing the fixed timestep interval also increases the “power” of physics because the timestep is running at a greater/more frequent interval? I’m working on an “angry birds”-style game, and my sprintjoint2D that seems to have increased “throwing power” at a higher interval so I was curious. (Obviously I can compensate by changing the sprjoint2D settings, lowering the tension so the object doesn’t fly so far.)
I can only assume the rigidbody2D was choppy/stuttering because of the code I’m using (again, affecting only the character’s rotation). I’m treating the object like an arrow flying through air, and it wasn’t stuttering until I put in code to make the object turn in the direction it was flying.
Running the code via FixedUpdate it stutters; running the code via Update it seems to be smooth but sometimes the calculated rotation amount is set to zero which causes the object to rotate out of whack several times a second. (degrees: 125, 126, 127, 0, 128 …) (I can avoid the “0” amount but that just goes back to the FixedUpdate effect of being choppy because it’s skipping frames)
Should I just stick with the increased Fixed Timestep since that seems to work (though affects the physics “power”), or does anyone here have different suggestions?
//for physics calculations
void FixedUpdate()
{
//constantly apply the right rotation to jonah
if (ArrowController.canClick == false && !hitGround)
{
//change jonah direction/rotation
RotationChangeWhileFlying();
}
}
//change the angular velocity depending on jonah's speed in air
void RotationChangeWhileFlying()
{
//get current position
currPoint = transform.position;
//get the direction (from previous pos to current pos)
currDir = prevPoint - currPoint;
//normalize the direction
currDir.Normalize();
//get angle whose tan = y/x, and convert from rads to degrees
float rotationZ = Mathf.Atan2(currDir.y, currDir.x) * Mathf.Rad2Deg;
//rotate z based on angle above + an offset (currently 90)
transform.rotation = Quaternion.Euler(0, 0, rotationZ + rotateOffset);
//store curr position as prev position for next frame
prevPoint = currPoint;
}
Here are some reference gifs (Imgur converted them automatically to MP4s), though it may be hard to tell normal vs stuttering because the recording software affected the recorded framerate.
Working version, running in FixedUpdate, increased Fixed Timestep from 0.02 to 0.005:
Stuttering framerate, running in FixedUpdate, Fixed Timestep is normal at 0.02:
__Imgur: The magic of the Internet
Framerate seems kind of smooth but rotation goes “wacky”/sometimes goes back to 0, running in Update, [u]Fixed Timestep is normal at 0.02:[/u]
__[u]Imgur: The magic of the Internet
