2016-10-30 76 views
0

當拖動相機時,我試圖使寬鬆或慣性減弱,因此當我放下相機時,它可以輕鬆放置到位。我想移動相機基於我扔/拖動它的力量。Unity相機平滑/鬆動拖動

這是我用來拖動相機的實際代碼,但沒有平滑的緩動。

using UnityEngine; 
using System.Collections; 

public class swipeCamera: MonoBehaviour 
{ 
    Vector3 hit_position = Vector3.zero; 
    Vector3 current_position = Vector3.zero; 
    Vector3 camera_position = Vector3.zero; 
    float z = 0.0f; 

    // Use this for initialization 
    void Start() 
    { 
    } 

    void Update() 
    { 
     if (Input.GetMouseButtonDown(0)) 
     { 
      hit_position = Input.mousePosition; 
      camera_position = transform.position; 

     } 
     if (Input.GetMouseButton(0)) 
     { 
      current_position = Input.mousePosition; 
      LeftMouseDrag(); 
     } 
    } 

    void LeftMouseDrag() 
    { 
     // From the Unity3D docs: "The z position is in world units from the camera." In my case I'm using the y-axis as height 
     // with my camera facing back down the y-axis. You can ignore this when the camera is orthograhic. 
     current_position.z = hit_position.z = camera_position.y; 

     // Get direction of movement. (Note: Don't normalize, the magnitude of change is going to be Vector3.Distance(current_position-hit_position) 
     // anyways. 
     Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position); 

     // Invert direction to that terrain appears to move with the mouse. 
     direction = direction * -1; 

     Vector3 position = camera_position + direction; 

     transform.position = position; 
    } 
} 

謝謝!

回答

0

緩動意味着隨着時間的推移緩慢移動。

所以這樣的行爲通常在Update方法中使用Vector.MoveTowards或其他lerp函數和Time.deltaTime來實現。

using UnityEngine; 
using System.Collections; 

public class swipeCamera: MonoBehaviour 
{ 
    public float speed = 2.0f;//easing speed 

    Vector3 hit_position = Vector3.zero; 
    Vector3 current_position = Vector3.zero; 
    Vector3 camera_position = Vector3.zero; 
    float z = 0.0f; 

    bool flag = false; 
    Vector3 target_position; 

    void Start() 
    { 
    } 

    void Update() 
    { 
     if (Input.GetMouseButtonDown(0)) 
     { 
      hit_position = Input.mousePosition; 
      camera_position = transform.position; 
     } 

     if (Input.GetMouseButton(0)) 
     { 
      current_position = Input.mousePosition; 
      LeftMouseDrag(); 
      flag = true; 
     } 

     if(flag) 
     { 
      transform.position = Vector3.MoveTowards(transform.position, target_position, Time.deltaTime*speed); 
      if(transform.position == target_position)//reached? 
      { 
       flag = false;// stop moving 
      } 
     } 
    } 

    void LeftMouseDrag() 
    { 
     // From the Unity3D docs: "The z position is in world units from the camera." In my case I'm using the y-axis as height 
     // with my camera facing back down the y-axis. You can ignore this when the camera is orthograhic. 
     current_position.z = hit_position.z = camera_position.y; 

     // Get direction of movement. (Note: Don't normalize, the magnitude of change is going to be Vector3.Distance(current_position-hit_position) 
     // anyways. 
     Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position); 

     // Invert direction to that terrain appears to move with the mouse. 
     direction = direction * -1; 

     target_position = camera_position + direction; 
    } 
} 
+0

zwcloud非常感謝你的工作就像一個魅力!真的很感謝你的幫助!你先生贏得了一個新朋友。 – PizzaMonster

+0

我很高興它有幫助。請記住在能夠時注意。 :) – zwcloud

+0

嘿傢伙,遲到的建議,但嘿,如果有人再讀一遍:) 我會使用'Vector3.Lerp()'。這使得攝像機運動更平滑,因爲它最終會緩慢下降。 'transform.position = Vector3.Lerp(transform.position,targetPos,Time.deltaTime * DragSpeed);' –