Unity3D Choppy Camera Motion

2019-04-17 08:29发布

问题:

I tried many different methods but never succeed to make a perfectly smooth camera movement. Even in a very simple scene, camera follow is not good enough. There are spikes in motion. Spikes do not occur periodically but randomly. If I didn't know some games(e.g. Manuganu) made in unity and has a perfect camera follow, I would think it is impossible.

What I have tried so far: -Change camera position in LateUpdate/FixedUpdate. -Made my target interpolate/extrapolate. -Moved camera depending on Deltatime. -Increased physics steps. -Set the targetframerate = 60. -Played with all Quality settings, Vsync etc. -Many variations and other things...

The best scenario is, random hiccups...happening in both mobile and editor. Unity's example scripts doesn't work perfectly smooth either. I can't continue developing sidescroll runner game because of this hiccup problem.

The question is: is there any way I didn't mention, is there any example on the Internet? I did everything I can do.

transform.position = new Vector3(target.x, target.y, -10);

This is how I update camera position in LateUpdate.

PS: There is no FPS drop issue.

回答1:

@Kashbel has half of it. You're using a fixed movement value but there is no guarantee that every frame takes the same amount of real world time. You need to make sure that the units-per-second is constant, which means you need to use the time to control the speed of the update.

The typical way to handle this is to use Time.DeltaTime, which will always be a tiny fraction representing the real world length of the frame in seconds. For example, as in the docs:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Update() {
        float translation = Time.deltaTime * 10;
        transform.Translate(0, 0, translation);
    }
}

here the script will move it's gameObject 10 units per second, regardless of framerate. Without the Time.deltaTime it might move 10 units in 1/60th of a second on one frame but 10 units in 1/15th of a second on a particularly slow frame.



回答2:

Try using the Lerp function instead directly moving the transform.position.

It would be something like transform.position = Vector3.Lerp(startPosition, endPosition, time);

It should be smooth using this. You have more information and examples at Vector3.Lerp Unity3D