【问题标题】:how to move Camera smoothly in unity3D?如何在unity3D中平滑移动相机?
【发布时间】:2014-01-20 18:48:08
【问题描述】:

当我想将Camera从原点移动到目标位置时,它看起来很僵硬。那么如果可以根据偏移量设置移动速度,怎么办?

【问题讨论】:

  • 你试过什么?如果您不发布任何代码,我们将无法知道您在做什么,也无法提出任何改进建议。
  • 请描述一下你的尝试!

标签: unity3d transform gameobject lerp


【解决方案1】:

关于这个问题有一个很好的教程,通常在unity网站上以of all tutorials开头描述。在Survival Shooter Tutorial里面有说明如何让摄像机在移动的同时平滑地移动到目标位置。

这是移动相机的代码。创建一个脚本,将其添加到相机,然后将您要移动到的GameObject 添加到脚本占位符中。它将自动保存转换组件,如脚本中的设置。 (在我的例子中,它是生存射击教程的玩家):

public class CameraFollow : MonoBehaviour
{
    // The position that that camera will be following.
    public Transform target; 
    // The speed with which the camera will be following.           
    public float smoothing = 5f;        

    // The initial offset from the target.
    Vector3 offset;                     

    void Start ()
    {
        // Calculate the initial offset.
        offset = transform.position - target.position;
    }

    void FixedUpdate ()
    {
        // Create a postion the camera is aiming for based on 
        // the offset from the target.
        Vector3 targetCamPos = target.position + offset;

        // Smoothly interpolate between the camera's current 
        // position and it's target position.
        transform.position = Vector3.Lerp (transform.position, 
                                           targetCamPos,   
                                           smoothing * Time.deltaTime);
    }
}

【讨论】:

    【解决方案2】:

    您可以使用iTween 插件。如果您希望您的相机与您的对象一起移动,即顺利地跟随它。您可以使用smoothFollow 脚本。

    【讨论】:

    • 我也会推荐这个,iTween 有很多选项可以平滑移动,从 cuadratic 和曲线可以使相机在开始时快速移动,然后在结束时减慢,编程可以是对于没有使用过 iTween 但确实值得的人感到困惑,joi,Nick's Answer 几乎就是这个问题的答案,一定要标记它。
    【解决方案3】:

    您是否尝试过使用 Vector3.MoveTowards?您可以指定要使用的步长,应该足够平滑。

    http://docs.unity3d.com/Documentation/ScriptReference/Vector3.MoveTowards.html

    【讨论】:

      【解决方案4】:
      public Vector3 target;        //The player
      public float smoothTime= 0.3f;  //Smooth Time
      private Vector2 velocity;       //Velocity
      Vector3 firstPosition;
      Vector3 secondPosition;
      Vector3 delta;
      Vector3 ca;
      tk2dCamera UICa;
      bool click=false;
      void Start(){
          ca=transform.position;
          UICa=GameObject.FindGameObjectWithTag("UI").GetComponent<tk2dCamera>();
      }
      void  FixedUpdate ()
      {
          if(Input.GetMouseButtonDown(0)){
      
      
              firstPosition=UICa.camera.ScreenToWorldPoint(Input.mousePosition);
              ca=transform.position;
      
          }
          if(Input.GetMouseButton(0)){
      
      
              secondPosition=UICa.camera.ScreenToWorldPoint(Input.mousePosition);
              delta=secondPosition-firstPosition;
              target=ca+delta;
      
          }
          //Set the position
          if(delta!=Vector3.zero){
              transform.position = new Vector3(Mathf.SmoothDamp(transform.position.x, target.x, ref velocity.x, smoothTime),Mathf.SmoothDamp( transform.position.y, target.y, ref velocity.y, smoothTime),transform.position.z);
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-21
        • 1970-01-01
        • 2012-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-23
        • 1970-01-01
        相关资源
        最近更新 更多