unity摄像机平滑移动旋转

public Camera camera;
public tranform tarPos;
public bool cameraMove=false;
private Vector3 velocity = Vector3.zero;
public Button btn;

void Start()
{
    btn.onClick.AddListener(delegate
    {
        cameraMove=true;
    });
}

void Update()
    {
        if (camerMove)
        {
            camera.position =
                Vector3.SmoothDamp (camera.position, tarPos.position, ref velocity, 2f);
            camera.rotation = Quaternion.RotateTowards( camera.rotation, tarPos.rotation,Time.deltaTime*20);
        }
    }

Unity中使用Vector3.SmoothDamp(平滑阻尼)方法进行跟随移动,可以使跟随看起来很平滑,而不显得突兀,最典型的示例就是相机平滑跟随角色移动。

SmoothDamp (current : Vector3, target : Vector3, ref currentVelocity : Vector3, smoothTime )
 

猜你喜欢

转载自blog.csdn.net/weixin_46051151/article/details/127026566