# Character Movement Stutter

Hello everyone! I’ve been a long time lurker and part time Unity tinkerer. I’ve been playing with Unity a bit more lately and bought a pre-made game to take apart and look into how things were done in Unity. It is the Last Stand top down shooter game, and I’ve noticed an issue that has been plaguing me for a while. i’ve tried re-working the code and couldn’t seem to solve the issue.

The issue i’m facing is that when the character moves, occasionally the character will stutter. It’s not a frame rate drop because the background and other characters move correctly, it’s like the characters body will suddenly jump back a few spaces then back to where it was. I posted an example video here:

I’ll put a bit of the movement code up as well, but I was wondering if anyone knows from just looking at it what could be the root issue? Or even a better way of doing the movement code so this won’t happen?

I don’t want to post the full script as it is part of this pre-purchased package, but the little snippet where the player is being moved should be fine:

``````function Update()
{
if( Input.GetKey("d") )
{
if ( SpeedX < MaxSpeedX ) //If you haven't reached the maximum SpeedX, keep accelerating
{
SpeedX += Acceleration; //Increase current SpeedX
}
else
{
SpeedX = MaxSpeedX; //If you've reached the maximum speed, stay at it
}

//Set rotation target for the player to be Right
RotateTarget = Vector3(270,180,0);
}
else if( Input.GetKey("a") )
{
if ( SpeedX > -MaxSpeedX ) //If you haven't reached the maximum SpeedX, keep accelerating
{
SpeedX -= Acceleration; //Increase current SpeedX
}
else
{
SpeedX = -MaxSpeedX; //If you've reached the maximum speed, stay at it
}

//Set rotation target for the player to be Left
RotateTarget = Vector3(270,0,0);
}
else //If you're not pressing "a" or "d", slow down
{
SpeedX *= 0.9; //decrease SpeedX
}

//Move the player in to the left/right based on his SpeedX,
transform.Translate(Vector3.right * SpeedX * Time.deltaTime, Space.World);
}
``````

Thanks for any help!

If you run a profiler alongside your project, you’ll probably notice Garbage Collection spikes whenever you stutter.

Thanks, I’ll have to look into this project in particular. You may be right, as I dont think it’s handling the destruction of objects all that well.

What prompted this question was that I have just started working with the iPhone and used the sample third person dual stick controller and only a few cubes in the scene and have experienced a similar stuttering problem. If it’s the garbage collector in that instance… I dont know why it would be causing that issue as there is almost nothing in the scene and nothing is being added or removed.

Perhaps it’s an issue with using the Update function? Should I be using something else or maybe not use translate to move the player? Just curious if anyone else has come upon a similar issue. If more info is needed I’ll gladly supply further code.