- Home /
Why is this stuttering so much?
I'm attaching an extremely simple movement script. The game is a white cube moving across a black screen and repeats. No spawning, no sounds, no special effects and no gimmicks with the camera.
There is just one camera and one object in the scene.
I have looked around for answers to the stuttering question and people are sending basic answers like using TimeDelta / some other method. The script in this project contains both known methods for linear movement:
Update() with TimeDelta and RigigBody2D velocity. Easily switchable in code, same results.
Can somebody please point out what the solution might be? If not, I'd like this project to kick off a serious discussion of rampant stuttering - especially on Android.
Using this script on a simple 64x64 object, landscape mode deployment
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
private Transform objTransform;
private Vector3 pos;
private Vector2 velocity = new Vector2(-6.0f, 0.0f);
private int movementMethod = 0;
// Use this for initialization
void Start () {
objTransform = gameObject.transform;
pos = objTransform.localPosition;
}
// Update is called once per frame
void Update () {
if(movementMethod == 1)
{
pos.x -= Time.deltaTime * 6;
if(pos.x < -7) {
pos.x = 7;
}
objTransform.localPosition = pos;
}
}
void FixedUpdate() {
if(movementMethod == 0)
{
this.rigidbody2D.velocity = velocity;
if(objTransform.localPosition.x < -7.0f)
{
objTransform.localPosition = new Vector3(7.0f, 0, 0);
}
}
}
}
Thank you for looking in to this. I believe this is really a major issue that is probably affecting so many people and I'm really surprised it isn't being discussed more.
I have fiddled around with target frame rate, vsync and any other settings that I could.
I've never seen any "stuttering" when manually moving objects that wasn't caused by excessive garbage collection or instantiating objects for the first time. Looks fine to me.
Physics by default is 50 fps, so if the framerate is higher, then physics won't update every frame. In which case you should use interpolation or increase the physics framerate.
Hello Eric5h5. Thanks for your response. Were you able to try this on Android? Look closely for a few $$anonymous$$utes.. On my end, I see this on my $$anonymous$$acBook Pro (in Editor) and Tab 3 7" / Nexus 7
Objects may appear to stutter when the distance they travel in a given frame is too great for the human eye to compensate for. Is it remotely possible that's what's going on for you?
I hope you're disabling your rigidbody before trying to use method 1.
The first thing I notice is that you're using localPosition with method == 1. The object's parent may be the one causing the stuttering in this case.
Here are some other things to try if you haven't already: Vector3.SmoothDamp, RigidBody2D.$$anonymous$$ovePosition, Vector3.Lerp
Have you made sure you experience the stuttering on your target platform? I find stuff always runs like crap in the editor but devices have no problem.
You may also want to try driving your object with forces ins$$anonymous$$d of velocity. Good luck!
unless 2 methods in unity for example physics are interfering, the transform is always smooth to the very pixel.
obviously you are telling the object to do more than change position.