- Home /
Why is my transform "stuttering" when I move it?
I know that there are several questions, but none of them could help me out :(
I have a Scene that just contains a Transform and the (non moving) Camera, and the only thing the Transform does is:
this.transform.Translate(Vector3.left * speed * Time.smoothDeltaTime);
if(this.transform.position.x < -200)
this.transform.position = new Vector3(200, this.transform.position.y, this.transform.position.z);
Of course this Codesample is in the "Update"Method, I tried it with fixedUpdate, lateUpdate and also with time.DeltaTime, fixedDeltaTime...and so on,
i tried it with Vsync off, vsync every vertical blank and every second vertical blank, but there was no option how to get rid of the stuttering.
Sometimes its a bit better, sometimes its horrible. Without vsync for example, it looks like some parts of the Transform are faster moving than others, and it looks very very "glitchy" - with vsync one i have constantly "jumps" in the movement.
My Screen is running at 60hZ. The drivers are up to date - and I am really frustrated in this topic :(
So if somebody could help me, it would be great! Kisses and hugs for the "problem-solver" :P
Have you seen this post and tried moving it like this :
http://answers.unity3d.com/questions/444978/c-smoothing-out-transformtranslate.html
i have seen it, anyway, I tried it again: Lerp doesn´t smooth it out (and it also geht´s slower at the end), and Vector3.$$anonymous$$oveTorwards doesn´t either...
:(
One thing that comes to $$anonymous$$d is that you're using Vector3.left which is shorthand for the global x-axis, but transform.Translate is actually relative to the object's local coordinate system when you omit the RelativeTo parameter.
I don't suppose there's something odd going on with the object's local space, i.e. is the transform childed to the camera, and the camera happens to have non-uniform or animated scaling?
well because Vector3.left is just (-1,0,0) i think it should work. But I´have also tried to set the transform coordinates directly, and also with moveTorwards.
The transform is not childed to the camera, the camera is the cam that gets created when you create a new scene, no changes, just orthogrpahic mode. The transform is just a plane with a texture on it, but I have the same results with a normal cube, and is not appended to the camera.
I have filmed the behaviour with my mobilephone-camera:
Answer by Landern · Nov 11, 2014 at 01:27 PM
Couple things, if you don't cache your transform the script is attached to, it's akin to using GetComponent each time you use 'this.transform', this can add up really quickly.
Since it hidden with in the text:
Little known fact: all of the component accessors in MonoBehaviour, things like transform, renderer, and audio, are equivalent to their GetComponent(Transform) counterparts, and they are actually a bit slow. GameObject.FindWithTag has been optimized, but in some cases, for example, in inner loops, or on scripts that run on a lot of instances, this script might be a bit slow.
Also it's better to copy the position struct, make modifications and assign it back, this directly applies to c#, though under the covers Java/UnityScript is performing this same action.
See if this is helpful.
using UnityEngine;
using System.Collections;
public class MovementText : MonoBehaviour {
float speed = 2.0f;
Transform me;
Transform cam;
// Use this for initialization
void Start () {
me = this.transform; // Cache Transform of script attached to object, otherwise GetComponent is used each time. This changes in Unity 5.
cam = Camera.main.transform;
}
// Update is called once per frame
void Update () {
me.Translate(Vector3.left * speed * Time.deltaTime, cam);
if(me.position.x < -200)
{
//Instantiating can be costly, copy the struct and reapply to object
Vector3 posCopy = me.position; // You retain the original values, modify what you need.
posCopy.x = 200;
me.position = posCopy;
}
}
}
from your video it looks like vsync issue. did you disabled vsync for better frame rate?
i tried it with vsync 0, 1 and 2.
there are difference, with vsync off it is very glitchy, with vsync 1 it stutters, and with vsync2 its almost horrible.
thanks for the hints - I tried it with your code, but it doesn´t seem to affect anythng.
I'm shooting in the dark here (which is why I never posted a definitive answer, but only comments. ;) ), but if you have Pro, or perhaps an active Pro trial, try opening your Profiler and see if there are any spikes that appear simultaneously with the stuttering. I can't imagine why there would be when the scene is otherwise totally empty, but you never know, could be something weird going on.
If you do see a profiler spike with the stutter, click it to view details for the frame where that spike occurred, then exa$$anonymous$$e the hierarchy to see what it is that takes longer.
@reppiz01 That code doesn't compile. targetPosition isn't declared. Is that really your full script?
reppiz: The RenderTexture.SetActive you're seeing in the profiler does not stem from your own code. Remember that the editor is itself a 3D application. The RenderTextures in use are the editor's windows themselves, e.g. Game + Scene.
Do a build of your game and run it externally, then see if the stuttering persists.
Answer by Nerigha · Nov 11, 2014 at 03:05 PM
Have you tried to implement physics for that object? Given a speed, you can determine the distance (current position of your object) in a given time. Speed = distance / time => distance = speed * time. I'll give you a short example:
public class script : MonoBehaviour
{
private float speed = -2f;
private Vector3 startPosition;
private float startTime;
private static Vector3 currentPosition;
void Start ()
{
startTime = Time.time;
startPosition = new Vector3 (this.transform.position.x, this.transform.position.y, this.transform.position.z);
}
void Update ()
{
float elapsedTime = Time.time - startTime;
currentPosition = new Vector3(startPosition.x + (speed * elapsedTime), startPosition.y, startPosition.z);
}
void LateUpdate ()
{
this.transform.position = currentPosition;
}
}
thanks. i tried your code (just with a bigger speed value), but the stuttering still persists.
I've added a light (directional), saved the scene and the project and build. Running it outside of unity seems to be ok, no stuttering. (Or my eyes are stuttering too :) )
tried to add directional light and made a webbuild. same stuttering as ever :( - i really run out of ideas how to fix this...