亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Unity實現(xiàn)場景漫游相機

 更新時間:2020年10月26日 13:13:16   作者:SlowFeather  
這篇文章主要為大家詳細介紹了Unity實現(xiàn)場景漫游相機,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Unity實現(xiàn)場景漫游相機的具體代碼,供大家參考,具體內容如下

前言

拿到場景后總喜歡在場景里面玩一段時間,那這個腳本就是你的不二選擇
代碼里加了注釋,改起來也很方便。

使用方法

把腳本拖拽到場景相機上,開箱即用。

  • WASD前后左右移動
  • QE為上下
  • Shift加速
  • 鼠標右鍵按住旋轉視角
  • ESC退出游戲

源碼

#if ENABLE_INPUT_SYSTEM && ENABLE_INPUT_SYSTEM_PACKAGE
#define USE_INPUT_SYSTEM
 using UnityEngine.InputSystem;
 using UnityEngine.InputSystem.Controls;
#endif

using UnityEngine;

 public class SimpleCameraController : MonoBehaviour
 {
  #region 相機狀態(tài)
  /// <summary>
  /// 相機狀態(tài)
  /// </summary>
  class CameraState
  {
   public float yaw;
   public float pitch;
   public float roll;
   public float x;
   public float y;
   public float z;

   public void SetFromTransform(Transform t)
   {
    pitch = t.eulerAngles.x;
    yaw = t.eulerAngles.y;
    roll = t.eulerAngles.z;
    x = t.position.x;
    y = t.position.y;
    z = t.position.z;
   }

   public void Translate(Vector3 translation)
   {
    Vector3 rotatedTranslation = Quaternion.Euler(pitch, yaw, roll) * translation;

    x += rotatedTranslation.x;
    y += rotatedTranslation.y;
    z += rotatedTranslation.z;
   }

   public void LerpTowards(CameraState target, float positionLerpPct, float rotationLerpPct)
   {
    yaw = Mathf.Lerp(yaw, target.yaw, rotationLerpPct);
    pitch = Mathf.Lerp(pitch, target.pitch, rotationLerpPct);
    roll = Mathf.Lerp(roll, target.roll, rotationLerpPct);
    
    x = Mathf.Lerp(x, target.x, positionLerpPct);
    y = Mathf.Lerp(y, target.y, positionLerpPct);
    z = Mathf.Lerp(z, target.z, positionLerpPct);
   }

   public void UpdateTransform(Transform t)
   {
    t.eulerAngles = new Vector3(pitch, yaw, roll);
    t.position = new Vector3(x, y, z);
   }
  }
  #endregion

  CameraState m_TargetCameraState = new CameraState();
  CameraState m_InterpolatingCameraState = new CameraState();

  [Header("Movement Settings 移動設置")]
  [Tooltip("Exponential boost factor on translation, controllable by mouse wheel. 平移的指數(shù)增強因子,可通過鼠標滾輪控制。")]
  public float boost = 3.5f;

  [Tooltip("Time it takes to interpolate camera position 99% of the way to the target. 將相機位置插值到目標位置99%所需的時間。"), Range(0.001f, 1f)]
  public float positionLerpTime = 0.2f;

  [Header("Rotation Settings 旋轉設定")]
  [Tooltip("X = Change in mouse position. 改變鼠標位置。\nY = Multiplicative factor for camera rotation. 相機旋轉的乘性因子。")]
  public AnimationCurve mouseSensitivityCurve = new AnimationCurve(new Keyframe(0f, 0.5f, 0f, 5f), new Keyframe(1f, 2.5f, 0f, 0f));

  [Tooltip("Time it takes to interpolate camera rotation 99% of the way to the target. 插值相機旋轉99%到目標所需的時間。"), Range(0.001f, 1f)]
  public float rotationLerpTime = 0.01f;

  [Tooltip("Whether or not to invert our Y axis for mouse input to rotation. 是否將鼠標輸入的Y軸反轉為旋轉。")]
  public bool invertY = false;

  void OnEnable()
  {
   m_TargetCameraState.SetFromTransform(transform);
   m_InterpolatingCameraState.SetFromTransform(transform);
  }

  Vector3 GetInputTranslationDirection()
  {
   Vector3 direction = new Vector3();
   if (Input.GetKey(KeyCode.W))
   {
    direction += Vector3.forward;
   }
   if (Input.GetKey(KeyCode.S))
   {
    direction += Vector3.back;
   }
   if (Input.GetKey(KeyCode.A))
   {
    direction += Vector3.left;
   }
   if (Input.GetKey(KeyCode.D))
   {
    direction += Vector3.right;
   }
   if (Input.GetKey(KeyCode.Q))
   {
    direction += Vector3.down;
   }
   if (Input.GetKey(KeyCode.E))
   {
    direction += Vector3.up;
   }
   return direction;
  }
  
  void Update()
  {
   Vector3 translation = Vector3.zero;

#if ENABLE_LEGACY_INPUT_MANAGER

   // Exit Sample 按下Esc鍵退出游戲
   if (Input.GetKey(KeyCode.Escape))
   {
    Application.Quit();
 #if UNITY_EDITOR
 UnityEditor.EditorApplication.isPlaying = false; 
 #endif
   }
   // Hide and lock cursor when right mouse button pressed 按下鼠標右鍵時隱藏并鎖定光標
   if (Input.GetMouseButtonDown(1))
   {
    Cursor.lockState = CursorLockMode.Locked;
   }

   // Unlock and show cursor when right mouse button released 松開鼠標右鍵時解鎖并顯示光標
   if (Input.GetMouseButtonUp(1))
   {
    Cursor.visible = true;
    Cursor.lockState = CursorLockMode.None;
   }

   // Rotation 旋轉
   if (Input.GetMouseButton(1))
   {
    var mouseMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y") * (invertY ? 1 : -1));
    
    var mouseSensitivityFactor = mouseSensitivityCurve.Evaluate(mouseMovement.magnitude);

    m_TargetCameraState.yaw += mouseMovement.x * mouseSensitivityFactor;
    m_TargetCameraState.pitch += mouseMovement.y * mouseSensitivityFactor;
   }
   
   // Translation 移動
   translation = GetInputTranslationDirection() * Time.deltaTime;

   // Speed up movement when shift key held 按住shift鍵時加速移動
   if (Input.GetKey(KeyCode.LeftShift))
   {
    //原速度*10為按下Shift后的速度
    translation *= 10.0f;
   }

   // Modify movement by a boost factor (defined in Inspector and modified in play mode through the mouse scroll wheel) 通過增強因子修改移動(在檢查器中定義,通過鼠標滾輪在播放模式下修改)
   boost += Input.mouseScrollDelta.y * 0.2f;
   translation *= Mathf.Pow(2.0f, boost);

#elif USE_INPUT_SYSTEM 
   // TODO: make the new input system work 使新的輸入系統(tǒng)正常工作
#endif

   m_TargetCameraState.Translate(translation);

   // Framerate-independent interpolation 幀率無關插值
   // Calculate the lerp amount, such that we get 99% of the way to our target in the specified time 計算lerp的數(shù)量,這樣我們就可以在指定的時間內到達目標的99%
   var positionLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / positionLerpTime) * Time.deltaTime);
   var rotationLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / rotationLerpTime) * Time.deltaTime);
   m_InterpolatingCameraState.LerpTowards(m_TargetCameraState, positionLerpPct, rotationLerpPct);

   m_InterpolatingCameraState.UpdateTransform(transform);
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#7.0中新特性匯總

    C#7.0中新特性匯總

    C#7.0 增加了許多新功能,并專注于數(shù)據(jù)消費,簡化代碼和性能的改善。接下來通過本文給大家介紹C#7.0中新特性匯總,需要的朋友可以參考下
    2016-08-08
  • 一文詳解C#?Chart控件

    一文詳解C#?Chart控件

    這篇文章主要介紹了一文學習C#?Chart控件,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • C#?壓榨cpu的辦法(推薦)

    C#?壓榨cpu的辦法(推薦)

    這篇文章主要介紹了C#?壓榨cpu的辦法,通過修改num的值,觀察cpu的核數(shù),例如我電腦是8核的,改成8,運行時各個核都能跑滿,感興趣的朋友跟隨小編一起看看吧
    2021-12-12
  • 解析C#中的常量及如何在C#編程中定義常量

    解析C#中的常量及如何在C#編程中定義常量

    這篇文章主要介紹了C#中的常量及如何在C#編程中定義常量,是C#入門學習中的基礎知識,需要的朋友可以參考下
    2016-01-01
  • C#獲取目錄最后訪問時間的方法

    C#獲取目錄最后訪問時間的方法

    這篇文章主要介紹了C#獲取目錄最后訪問時間的方法,涉及C#中LastAccessTime方法的使用技巧,需要的朋友可以參考下
    2015-04-04
  • 淺析WPF中Popup彈出層的使用

    淺析WPF中Popup彈出層的使用

    這篇文章將通過一個簡單的小例子,為大家詳細介紹一下如何在WPF開發(fā)中,通過Popup實現(xiàn)鼠標點擊彈出浮動停靠窗口,有需要的小伙伴可以了解下
    2024-01-01
  • 使用C#給PDF文檔添加注釋的實現(xiàn)代碼

    使用C#給PDF文檔添加注釋的實現(xiàn)代碼

    本文將實例講述C#中如何使用免費組件給PDF文檔添加文本注釋,包括自由文本注釋。自由文本注釋能允許我們自定義它的風格和外觀,非常具有實用價值
    2017-01-01
  • C# 使用Log4net添加日志記錄的方法

    C# 使用Log4net添加日志記錄的方法

    本文主要介紹了C# 使用Log4net添加日志記錄的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • C#中的Internal關鍵字小結

    C#中的Internal關鍵字小結

    這篇文章主要介紹了C#中的Internal關鍵字小結的相關資料,需要的朋友可以參考下
    2017-05-05
  • C#難點逐個擊破(5):類的訪問類型

    C#難點逐個擊破(5):類的訪問類型

    類的訪問類型有時也叫訪問級別,使用以下訪問修改符:Public、Protected、Private、internal、protected internal。
    2010-02-02

最新評論