Search Unity

Character Movement Stutter

Discussion in 'Editor & General Support' started by amindler, Jan 16, 2012.

  1. amindler

    amindler

    Joined:
    May 21, 2010
    Posts:
    19
    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:
    http://youtu.be/NhWutif0VbY?hd=1

    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:

    Code (csharp):
    1.  
    2. function Update()
    3. {
    4. if( Input.GetKey("d") )
    5.         {
    6.             if ( SpeedX < MaxSpeedX ) //If you haven't reached the maximum SpeedX, keep accelerating
    7.             {
    8.                 SpeedX += Acceleration; //Increase current SpeedX
    9.             }
    10.             else
    11.             {
    12.                 SpeedX = MaxSpeedX; //If you've reached the maximum speed, stay at it
    13.             }
    14.            
    15.             //Set rotation target for the player to be Right
    16.             RotateTarget = Vector3(270,180,0);
    17.         }  
    18.         else if( Input.GetKey("a") )
    19.         {
    20.             if ( SpeedX > -MaxSpeedX ) //If you haven't reached the maximum SpeedX, keep accelerating
    21.             {
    22.                 SpeedX -= Acceleration; //Increase current SpeedX
    23.             }
    24.             else
    25.             {
    26.                 SpeedX = -MaxSpeedX; //If you've reached the maximum speed, stay at it
    27.             }
    28.            
    29.             //Set rotation target for the player to be Left
    30.             RotateTarget = Vector3(270,0,0);
    31.         }
    32.         else //If you're not pressing "a" or "d", slow down
    33.         {
    34.             SpeedX *= 0.9; //decrease SpeedX
    35.         }
    36.        
    37.         //Move the player in to the left/right based on his SpeedX,
    38.         transform.Translate(Vector3.right * SpeedX * Time.deltaTime, Space.World);
    39. }
    40.  
    Thanks for any help!
     
  2. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    If you run a profiler alongside your project, you'll probably notice Garbage Collection spikes whenever you stutter.
     
  3. amindler

    amindler

    Joined:
    May 21, 2010
    Posts:
    19
    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.