2016-10-30 16 views
0

카메라를 드래그 할 때 여유 공간이나 관성을 만들려고 노력하므로 카메라를 떨어 뜨리면 공간이 줄어 듭니다. 나는 던지기/끌기하는 힘에 기초하여 카메라를 움직이고 싶습니다.유니티 카메라 부드럽고 여유롭게 드래그

이것은 카메라를 끌기 위해 사용하는 실제 코드이지만 부드럽게 감속하는 것은 아닙니다.

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

시간이 지남에 따라 점감 적으로 이동하는 것을 의미합니다.

그런 행동은 일반적으로 Vector.MoveTowards 또는 다른 lerp 함수로 구현되고 은 Update 메서드로 구현됩니다.

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

도움이 되었기 때문에 기쁩니다. 당신이 할 수있을 때 upvote에 기억하십시오. :) – zwcloud

+0

안녕 얘들 아, 늦은 제안하지만, 만약 누군가가 이것을 다시 읽으면 헤이 : 나는'Vector3.Lerp()'를 사용할 것이다. 이것은 천천히 끝날 것이기 때문에 더 부드러운 카메라 움직임을 제공합니다. 'transform.position = Vector3.Lerp (transform.position, targetPos, Time.deltaTime * DragSpeed);' –