顺利地看不同的对象

时间:2016-05-16 11:10:08

标签: c# unity3d

如图所示,我的相机在路径上移动,我想要顺利地看到路径的不同对象。目前我已经尝试了

trasnform.LooAt(activePath.gameObject.transform);

但它产生了不稳定的结果。对象突然看到下一个对象与混蛋!如何避免它。我搜索并找到了这个解决方案,但它也无法正常工作

var targetRotation = Quaternion.LookRotation(activePath.gameObject.transform.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime);

enter image description here

2 个答案:

答案 0 :(得分:0)

void SmoothLookAt(Vector3 newDirection){ transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(newDirection), Time.deltaTime); }

将目标新外观的Vector3位置传递给此方法,它将平滑地查看目标。

e.g。 SmoothLookAt(activePath.gameObject.transform)

答案 1 :(得分:0)

这是一种平滑翻译的方法。参数是

  • 要翻译的对象转换,
  • 最终职位和
  • lookAt transform


private const float ANIMATION_DURATION_IN_SECONDS = 5f;

private IEnumerator SmoothTranslation(Transform startTransform, Vector3 finalPosition, Transform lookAtTransform){
        float currentDelta = 0;

        // Store initial values, because startTransform is passed by reference
        var startPosition = startTransform.position;
        var startRotation = startTransform.rotation;

        while (currentDelta <= 1f) {
            currentDelta += Time.deltaTime / ANIMATION_DURATION_IN_SECONDS;

            transform.position = Vector3.Lerp(startPosition, finalPosition, currentDelta);

            if (lookAtTransform != null) {
                // HACK Trick: 
                // We want to rotate the camera transform at 'lookAtTransform', so we do that.
                // On every frame we rotate the camera to that direction. And to have a smooth effect we use lerp.
                // The trick is even when we know where to rotate the camera, we are going to override the rotation 
                // change caused by `transform.lookAt` with the rotation given by the lerp operation. 
                transform.LookAt(lookAtTransform);
                transform.rotation = Quaternion.Lerp(startRotation, transform.rotation, currentDelta);
            }
            else {
                Debug.LogWarning("CameraZoomInOut: There is no rotation data defined!");
                transform.rotation = originalCameraRotation;
            }

            yield return null;
        }

    }