2016-05-16 44 views
0

如圖所示,我的相機在路徑上移動,我想順利地查看路徑的不同對象。目前我已經嘗試平滑地查看不同的對象

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

回答

0

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

傳中,重新審視目標,以這種方法的Vector3位置,它會順利看目標。

例如SmoothLookAt(activePath.gameObject.transform)

+0

它不工作,而不是急拉不斷 –

+0

嘗試添加乘以Time.deltaTime來減慢移動速度,直到指向你的i ssue。我擔心還有其他腳本會導致問題。您可能需要發佈更多的腳本示例 – Harvey

+0

實際上我使用的是megaShape軟件包,它可以獲取spl格式樣條然後我將我的相機逐一移動到這些樣條線上。更多http://stackoverflow.com/questions/37249480/rotation-toward-the-movement-direction-of-object –

0

這裏有一個方法來平滑翻譯。該參數是

  • 對象進行翻譯轉換,
  • 最終位置和
  • 的lookAt變換


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; 
     } 

    }