最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity實(shí)現(xiàn)場(chǎng)景漫游相機(jī)

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

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

前言

拿到場(chǎng)景后總喜歡在場(chǎng)景里面玩一段時(shí)間,那這個(gè)腳本就是你的不二選擇
代碼里加了注釋,改起來(lái)也很方便。

使用方法

把腳本拖拽到場(chǎng)景相機(jī)上,開箱即用。

  • WASD前后左右移動(dòng)
  • QE為上下
  • Shift加速
  • 鼠標(biāo)右鍵按住旋轉(zhuǎn)視角
  • 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 相機(jī)狀態(tài)
  /// <summary>
  /// 相機(jī)狀態(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 移動(dòng)設(shè)置")]
  [Tooltip("Exponential boost factor on translation, controllable by mouse wheel. 平移的指數(shù)增強(qiáng)因子,可通過(guò)鼠標(biāo)滾輪控制。")]
  public float boost = 3.5f;

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

  [Header("Rotation Settings 旋轉(zhuǎn)設(shè)定")]
  [Tooltip("X = Change in mouse position. 改變鼠標(biāo)位置。\nY = Multiplicative factor for camera rotation. 相機(jī)旋轉(zhuǎn)的乘性因子。")]
  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. 插值相機(jī)旋轉(zhuǎn)99%到目標(biāo)所需的時(shí)間。"), Range(0.001f, 1f)]
  public float rotationLerpTime = 0.01f;

  [Tooltip("Whether or not to invert our Y axis for mouse input to rotation. 是否將鼠標(biāo)輸入的Y軸反轉(zhuǎn)為旋轉(zhuǎn)。")]
  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 按下鼠標(biāo)右鍵時(shí)隱藏并鎖定光標(biāo)
   if (Input.GetMouseButtonDown(1))
   {
    Cursor.lockState = CursorLockMode.Locked;
   }

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

   // Rotation 旋轉(zhuǎn)
   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 移動(dòng)
   translation = GetInputTranslationDirection() * Time.deltaTime;

   // Speed up movement when shift key held 按住shift鍵時(shí)加速移動(dòng)
   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) 通過(guò)增強(qiáng)因子修改移動(dòng)(在檢查器中定義,通過(guò)鼠標(biāo)滾輪在播放模式下修改)
   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 幀率無(wú)關(guān)插值
   // Calculate the lerp amount, such that we get 99% of the way to our target in the specified time 計(jì)算lerp的數(shù)量,這樣我們就可以在指定的時(shí)間內(nèi)到達(dá)目標(biāo)的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);
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#如何使用Task類解決線程的等待問(wèn)題

    C#如何使用Task類解決線程的等待問(wèn)題

    這篇文章主要介紹了C#如何使用Task類解決線程的等待問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • C#深拷貝方法探究及性能比較(多種深拷貝)

    C#深拷貝方法探究及性能比較(多種深拷貝)

    這篇文章主要介紹了C#中使用NetCDF存儲(chǔ)二維數(shù)據(jù)的讀寫操作簡(jiǎn)單應(yīng)用,探究了以下幾種C#對(duì)象深拷貝方式,同時(shí)簡(jiǎn)單對(duì)比了以下列出的幾種深拷貝方式的速度,需要的朋友可以參考下
    2022-04-04
  • C#處理類型和二進(jìn)制數(shù)據(jù)轉(zhuǎn)換并提高程序性能

    C#處理類型和二進(jìn)制數(shù)據(jù)轉(zhuǎn)換并提高程序性能

    這篇文章介紹了C#處理類型和二進(jìn)制數(shù)據(jù)轉(zhuǎn)換并提高程序性能的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • C#凈化版WebApi框架的實(shí)現(xiàn)

    C#凈化版WebApi框架的實(shí)現(xiàn)

    這篇文章主要介紹了C#凈化版WebApi框架的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-03-03
  • C#實(shí)現(xiàn)將PDF轉(zhuǎn)為線性化PDF

    C#實(shí)現(xiàn)將PDF轉(zhuǎn)為線性化PDF

    線性化PDF文件是PDF文件的一種特殊格式,可以通過(guò)Internet更快地進(jìn)行查看。這篇文章主要介紹了如何通過(guò)C#實(shí)現(xiàn)將PDF轉(zhuǎn)為線性化PDF,感興趣的小伙伴可以學(xué)習(xí)一下
    2021-12-12
  • C# 字符串的連接(實(shí)例講解)

    C# 字符串的連接(實(shí)例講解)

    下面小編就為大家分享一篇C# 字符串的連接實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • C#面向?qū)ο缶幊讨欣锸咸鎿Q原則的示例詳解

    C#面向?qū)ο缶幊讨欣锸咸鎿Q原則的示例詳解

    在面向?qū)ο缶幊讨?,SOLID?是五個(gè)設(shè)計(jì)原則的首字母縮寫,旨在使軟件設(shè)計(jì)更易于理解、靈活和可維護(hù)。本文將通過(guò)實(shí)例詳細(xì)講講C#面向?qū)ο缶幊讨欣锸咸鎿Q原則,需要的可以參考一下
    2022-07-07
  • c# 服務(wù)器上傳木馬監(jiān)控代碼(包含可疑文件)

    c# 服務(wù)器上傳木馬監(jiān)控代碼(包含可疑文件)

    c# 監(jiān)控服務(wù)器上傳木馬(包含可疑文件)
    2010-05-05
  • 基于C#制作一個(gè)顏色拾取器

    基于C#制作一個(gè)顏色拾取器

    這篇文章主要為大家詳細(xì)介紹了如何基于C#制作一個(gè)顏色拾取器,可以獲取屏幕上任意位置像素的色值,文中的示例代碼講解詳細(xì),有需要的小伙伴可以了解下
    2024-01-01
  • C#實(shí)現(xiàn)矩陣乘法實(shí)例分析

    C#實(shí)現(xiàn)矩陣乘法實(shí)例分析

    這篇文章主要介紹了C#實(shí)現(xiàn)矩陣乘法的方法,實(shí)例分析了通過(guò)C#數(shù)組構(gòu)造矩陣及實(shí)現(xiàn)矩陣乘法的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04

最新評(píng)論

翁牛特旗| 崇仁县| 云梦县| 海晏县| 贵溪市| 苗栗县| 苗栗县| 师宗县| 法库县| 宁晋县| 察隅县| 屏山县| 昭苏县| 卢龙县| 拉萨市| 阿瓦提县| 乐业县| 乌拉特后旗| 临城县| 襄樊市| 武穴市| 神农架林区| 广汉市| 蒙山县| 高阳县| 潜江市| 北辰区| 平阴县| 灵山县| 云安县| 东乌珠穆沁旗| 清苑县| 通榆县| 凤山县| 都江堰市| 波密县| 抚宁县| 湘西| 平塘县| 永福县| 丰县|