I’m using the 2d animation package to animate the characters in my game using skeletal animations. I’m running into an issue I don’t fully understand.
The camera follows around the main character in the game. This all works fine, but when I added smoothing to the camera movement, I started getting a weird stutter in the animations.
Here’s how it looks without any smoothing:
281y7a
And here’s how it looks with smoothing.
oa3jrj
The effect may appear subtle in the video, but it’s very noticable and annoying in game.
This is the code I use for the camera movement/smoothing:
// Determine next position. extraOffset is a static offset configured for the object in the editor.
var desiredPos = new Vector3(followObject.transform.localPosition.x, followObject.transform.localPosition.y, transform.position.z) + extraOffset;
//Apply smoothing
desiredPos = Vector2.Lerp(transform.position, desiredPos, smoothSpeed * Time.deltaTime);
var newPos = new Vector3(desiredPos.x, desiredPos.y, transform.position.z);
transform.position = newPos;
I checked the profiler to see if the stuttering is caused by a bad framerate, but both run at solid 60FPS. I also checked the scene view during the test, and noticed that the stuttering doesn’t happen in the scene camera, so I’m sure it’s related to the camera somehow, but as I said, I don’t fully understand this issue. Any pointers in the right direction would be very much appreciated!
Delete your character, make him a cube (or sprite) without animation. Does the issue go away? If so then there’s something going on with the animations.
If not, there’s something going on with your computation above. It seems okay at first glance, but it might have some other issue.
You should be able to eliminate smoothing by changing the smoothSpeed * Time.deltaTime
term to be 1.0f
just to see if it works as before.
ALSO: do not mix Update() and FixedUpdate(), as those happen in different timecycles.
Here is some timing diagram help:
Thanks for your reply, I tried disabling the animator (as you more or less suggested). Apparently the issue is still present without it. It seems indeed that it’s not related to the animation. So somehow it relates to the character movement in combination with the camera smoothing. I’ll do some tests to see if I can pinpoint the issue now.
Alright, I solved it.
Apparently the character movement wasn’t completely smooth. Since the camera followed the character smoothly, you suddenly notice any inconsistencies between the cam and the character. This wasn’t noticable when the cam exactly followed the character. (it’s a bit like being inside the back of a truck, you can’t see how fast you move since your frame of reference moves along with you, similarly I wasn’t able to notice the movement stutters because the cam stuttered along with it).
The reason character movement wasn’t smooth had to do with how they move between cells. Basically, I lerped the character from one cell to another. Since the game updates in frames, it happened that there’s still some frame time left when the character would reach the next cell. Basically, the last lerp would be clamped to the target cell pos, and the character would move slightly less far in the last frame of a cell move. I fixed this by carrying over the last frame to the next cell move.
1 Like