- Home /
Choppy movment for even the most basic stuff
I'm using the following code for updating my player.
void Update () {
float hori = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
Vector2 transVector = new Vector2(hori, ver);
transform.Translate(transVector * 5 * Time.deltaTime);
}
But when running the movment is choppy, should I say, jumpy, because it shakes left and right.
I then created a new project with only a single cube in it and the same script. But it is still choppy when hit run, noted the fps is at 60. This happens intermitently and isn't that serious, but still, even the slightly choppy moment is unbearable since this is such a simple scene.
Note: only translation is suffering from this choppy movement, all other animations are rendered smoothly in the game. So I can only think of this as not updating in constant frame. But I already have Update and DeltaTime used, why will this happen?
Please advise.
Answer by Owen-Reynolds · Jun 08, 2013 at 03:28 PM
Do you have a camera targeting the player? If so, try it with a fixed (script-less) camera. Sometimes the jerkiness is really in the camera positioning.
Sometimes the editor, will be a little jerky. It's handling the extra windows that won't be in the real game. That comes and goes. If you try a Build&Run you'll see the "real" look of the game.
Otherwise, your code runs nice and smooth for me.
I first have a camera targeting the player by making it a child, and then I noticed the jerkyness and detached it. Now I found out that the jerkyness comes from the player translation.
Since I'm making a 2D game and I'm on a high-end alienware laptop so I assume that won't be too much overhead.
However, I guess this might have something to do with my video card: Raedom HD 6970, a pretty good one, but it's about to die: it cranks up to 100C just by playing some clip, and often has driver crash, frame rate drop etc, etc. Not sure if this actually affects the game since I tried my project on another desktop computer with nvidia card and it's having the same issue (unless they are both bad...)
BTW how do I build and run from the editor?
But does the camera have a script targeting the player?
I've found "editor jerk" is very intermittent, so probably not that if the player jerking is consistent. But, Click Edit-> Build; add your scene; click build&run.
If the bad computer was the problem, everything would probably have the same jerkiness.
Is it spraying errors as it runs? But that would also result in a global frame-rate issue, not just the player.
I used to, but then I removed the script and attached it to the player as a child so it follows natually.
$$anonymous$$ove over, I move the script and detached the camera so it's not following the player at all and I see jerky movement on the player.
It's not spraying errors of any kind, I guess the problem is the "editor jerk", tho it happens only on translation on certain object (says if I have a slighly complex scene have have object translating at different speed in the background, some will have jerky movement and some don't, again they all use the same translate method).
Also I cannot find Build under Edit in Unity editor, only Play and some other options.
I'd guess you've got something set funny somewhere. $$anonymous$$aybe start a fresh scene, don't even touch the camera except to aim it, and put that script on a cube. I'd imagine it will look fine (it did for me.) Then back-engineer what was wrong in the original scene.
The editor doesn't have any special problems with Translate -- it just sometimes can drop frames a little more that playing for real, or have a funny border, and very rarely. Build is in File (sorry,) but I doubt that's it.
I build it and it worked smoothly. Thanks! But I also have a question: for the update option there is Update and FixedUpdate, and I know the differences: FixedUpdate is called per time interval and Update is called per frame? If that's the case there is no need to use deltaTime in FixedUpdate right? In fact why should there be deltaTime at all? Just put everything in FixedUpdate and leave the Update blank since it's not framerate stable.
Answer by bubzy · Jun 08, 2013 at 02:18 PM
you might try this http://docs.unity3d.com/Documentation/ScriptReference/Vector2.Lerp.html
$$anonymous$$oving with Lerp assumes you have some known target point and want to gradually reach it over several frames by adding a constant amount. For example, if tapping an arrow key made you slide over to the next board position.
Also, Lerp is just math -- it doesn't have any secret smoothing sauce. pos=Lerp(A,B, 0.3f);
is just a shortcut for pos = A + (B-A)*0.3f;
.
Your answer
Follow this Question
Related Questions
Movement using Time.deltaTime not working on Fast mac 3 Answers
Moving objects performance in PC and Editor 0 Answers
Movement lag, jitery every few seconds 1 Answer
Disable Diagonal Move 2 Answers