Unity C# 一些關於Camera的心得!

来源:http://www.cnblogs.com/AdvancePikachu/archive/2017/05/15/6856374.html
-Advertisement-
Play Games

本文原創,轉載請註明出處:http://www.cnblogs.com/AdvancePikachu/p/6856374.html 首先,總結了下最近工作中關於攝像機漫游的功能, 腳本如下: 1 Transform _Camera; 2 public LayerMask mask; 3 4 publ ...


本文原創,轉載請註明出處:http://www.cnblogs.com/AdvancePikachu/p/6856374.html

首先,總結了下最近工作中關於攝像機漫游的功能,

腳本如下:

  1     Transform _Camera;
  2     public LayerMask mask;
  3      
  4     public float checkHeight = 500f;
  5     public float minHeight = 20f;
  6     public float maxHeight = 8000f;
  7     public float minClamp = 50f;
  8     public float maxClamp = 950f;
  9 
 10     public float sensitivityX = 5f;
 11     public float sensitivityY = 5f;
 12     private float rotationY = 0f;
 13 
 14     //上下最大Y視角
 15     public float minimumY = -90f;
 16     public float maximumY = 30f;
 17 
 18     public Vector3 PreMouseMPos;
 19 
 20     public float scrollSpeed = 200f;
 21 
 22     void Start () 
 23     {
 24         mask.value = 1;
 25         _Camera = Camera.main.transform;
 26     }
 27     
 28     // Update is called once per frame
 29     void Update ()
 30     {
 31         if(Input.GetKey(KeyCode.LeftAlt))
 32         {
 33             MouseScrollWheel ();
 34             CameraMove ();
 35             MoveEulerAngles ();
 36         }
 37     }
 38 
 39     /// <summary>
 40     /// Checks the height of the low.
 41     /// </summary>
 42     void CheckLowHeight()
 43     {
 44         if (_Camera.position.x < minClamp)
 45             _Camera.position = new Vector3 (minClamp, _Camera.position.y, _Camera.position.z);
 46         if (_Camera.position.x > maxClamp)
 47             _Camera.position = new Vector3 (maxClamp, _Camera.position.y, _Camera.position.z);
 48         if (_Camera.position.z < minClamp)
 49             _Camera.position = new Vector3 (_Camera.position.x, _Camera.position.y, minClamp);
 50         if (_Camera.position.z > maxClamp)
 51             _Camera.position = new Vector3 (_Camera.position.x, _Camera.position.y, maxClamp);
 52 
 53         RaycastHit hit;
 54         if(Physics.Raycast(_Camera.position+Vector3.up*checkHeight,Vector3.down,out hit ,checkHeight+minHeight,mask))
 55         {
 56             if(_Camera.position.y-hit.point.y<minClamp)
 57             {
 58                 Vector3 lowPoint = hit.point + new Vector3 (0, minHeight, 0);
 59                 _Camera.position = lowPoint;
 60             }
 61         }
 62     }
 63 
 64     /// <summary>
 65     /// Mouses the scroll wheel.
 66     /// </summary>
 67     private void MouseScrollWheel()
 68     {
 69         if(Input.GetAxis("Mouse ScrollWheel")!=0)
 70         {
 71             _Camera.Translate (new Vector3 (0, 0, Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * scrollSpeed));
 72         }
 73 
 74         CheckLowHeight ();
 75 
 76         if (_Camera.position.y >= maxHeight)
 77             _Camera.position = new Vector3 (_Camera.position.x, maxHeight, _Camera.position.z);
 78     }
 79 
 80     /// <summary>
 81     /// Cameras the move.
 82     /// </summary>
 83     private void CameraMove()
 84     {
 85         if(Input.GetMouseButton(0))
 86         {
 87             if(PreMouseMPos.x<=0)
 88             {
 89                 PreMouseMPos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0);
 90             }
 91             else
 92             {
 93                 Vector3 CurMouseMPos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0);
 94                 Vector3 offset = CurMouseMPos - PreMouseMPos;
 95                 offset = -offset * 0.5f * 2;
 96                 if((_Camera.position+offset).y>=minHeight
 97                     &&(_Camera.position+offset).y<=maxHeight)
 98                 {
 99                     _Camera.Translate(offset);
100 
101                     CheckLowHeight();
102 
103                     PreMouseMPos = CurMouseMPos;
104                 }
105             }                
106         }
107         else
108             PreMouseMPos = Vector3.zero;
109     }
110 
111     /// <summary>
112     /// Moves the euler angles.
113     /// </summary>
114     private void MoveEulerAngles()
115     {
116         if(Input.GetMouseButton(1))
117         {
118             float rotationX = _Camera.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX;
119 
120             rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
121 
122             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
123 
124             _Camera.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
125         }
126     }
MoveRocket

第二個是關於攝像機的限制範圍和遇到障礙物自動變換與target的距離的功能

代碼如下:

  1 public Transform target;
  2 
  3     public LayerMask mask = new LayerMask();
  4 
  5     public Vector2 targetOffset = new Vector2();
  6     public Vector2 originRotation = new Vector2();
  7 
  8     public float distance = 5;
  9     public float minDistance = 0;
 10     public float maxDistance = 10;
 11 
 12     public Vector2 sensitivity = new Vector2(3, 3);
 13 
 14     public float zoomSpeed = 1;
 15     public float zoomSmoothing = 16;
 16 
 17     public float minAngle = -90;
 18     public float maxAngle = 90;
 19 
 20     private float _zoom_in_timer = 0;
 21     private float _zoom_out_timer = 0;
 22 
 23     private float _wanted_distance;
 24     private Quaternion _rotation;
 25     private Vector2 _input_rotation;
 26 
 27     private Transform _t;
 28 
 29     void Start()
 30     {
 31         mask.value = 1;
 32         _t = transform;
 33         _wanted_distance = distance;
 34         _input_rotation = originRotation; 41     }
 42 
 43     void Update()
 44     {
 45         if (target) {
 46 
 47             if (Input.GetMouseButton (0) || Input.GetMouseButton (1)) {
 48                 if (Input.GetAxis ("Mouse X") != 0 || Input.GetAxis ("Mouse Y") != 0) {
 49                     if (!Cursor.visible) {
 50                         Cursor.visible = false;
 51                         Cursor.lockState = CursorLockMode.Locked;
 52                     }
 53                 }
 54 
 55                 return;
 56             }
 57         }
 58 
 59 
 60         if (!Cursor.visible) {
 61             Cursor.visible = true;
 62             Cursor.lockState = CursorLockMode.None;
 63         }
 64 
 65     }
 66     void FixedUpdate()
 67     {
 68         if(target)
 69         {
 70 
 71             // Zoom control
 72             if(Input.GetAxis("Mouse ScrollWheel") < 0 )
 73             {
 74                 _wanted_distance += zoomSpeed;
 75             }
 76             else if(Input.GetAxis("Mouse ScrollWheel") > 0 )
 77             {
 78                 _wanted_distance -= zoomSpeed;
 79             }
 80 
 81 
 82             _wanted_distance = Mathf.Clamp(_wanted_distance, minDistance, maxDistance);
 83 
 84 
 85             if (Input.GetMouseButton (0) || Input.GetMouseButton (1)) {
 86 
 87                 _input_rotation.x += Input.GetAxis ("Mouse X") * sensitivity.x;
 88 
 89 
 90                 ClampRotation ();
 91 
 92 
 93                 _input_rotation.y -= Input.GetAxis ("Mouse Y") * sensitivity.y;
 94 
 95 
 96                 _input_rotation.y = Mathf.Clamp (_input_rotation.y, minAngle, maxAngle);
 97 
 98                 _rotation = Quaternion.Euler (_input_rotation.y, _input_rotation.x, 0);
 99 
100             }
101 
102             // Lerp from current distance to wanted distance
103             distance = Mathf.Clamp(Mathf.Lerp(distance, _wanted_distance, Time.deltaTime * zoomSmoothing), minDistance, maxDistance);
104 
105             // Set wanted position based off rotation and distance
106             Vector3 wanted_position = _rotation * new Vector3(targetOffset.x, 0, -_wanted_distance - 0.2f) + target.position + new Vector3(0, targetOffset.y, 0);
107             Vector3 current_position = _rotation * new Vector3(targetOffset.x, 0, 0) + target.position + new Vector3(0, targetOffset.y, 0);
108 
109 
110             // Linecast to test if there are objects between the camera and the target using collision layers
111             RaycastHit hit;
112 
113             if(Physics.Linecast(current_position, wanted_position, out hit, mask))
114             {
115                 distance = Vector3.Distance(current_position, hit.point) - 0.2f;
116             }
117 
118 
119             // Set the position and rotation of the camera
120             _t.position = _rotation * new Vector3(targetOffset.x, 0.0f, -distance) + target.position + new Vector3(0, targetOffset.y, 0);
121             _t.rotation = _rotation;
122         }
123     }
124 
125     private void ClampRotation()
126     {
127         if(originRotation.x < -180)
128         {
129             originRotation.x += 360;
130         }
131         else if(originRotation.x > 180)
132         {
133             originRotation.x -= 360;
134         }
135 
136         if(_input_rotation.x - originRotation.x < -180)
137         {
138             _input_rotation.x += 360;
139         }
140         else if(_input_rotation.x - originRotation.x > 180)
141         {
142             _input_rotation.x -= 360;
143         }
144     }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 經常在項目中遇到定時任務的時候,通常第一個想到的是Timer定時器,但是這玩意功能太弱雞,實際上通常採用的是專業化的第三方調度框架,比如說 Quartz,它具有功能強大和應用的靈活性,我想使用過的人都非常瞭解,那麼本篇就來說說如何通過代碼和配置文件來進行job和trigger的配置。 一:常規的jo ...
  • 今天學習angularjs向資料庫添加數據。學習此篇,得從以往幾篇開始,因為那還有創建數據表等演示。現在來創建一個添加的存儲過程: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[usp_Goods_I ...
  • c 介面使用詳解 c 中介面隱式與顯示實現 c 中介面可以隱式實現、顯示實現,隱式實現更常使用、顯示實現較少使用 其區別在於 1. 顯示實現避免介面函數簽名衝突 2. 顯示實現只可以以介面形式調用 3. 顯示實現其子類需無法繼承介面 4. 隱式實現可以撕裂調用、可以介面調用 5. 隱式實現其子類可以 ...
  • 下載並引入兩個dll文件 NPinyin.dll 和 ChnCharInfo.dll 其實這兩個dll 任何一個都可以實現漢字轉拼音,然而 NPinyin.dll 收錄的漢字並不全,但是很人性化,能識別一些常用的漢字。ChnCharInfo.dll 是微軟的很全但是不人性化。另外本套代碼外有一個自己 ...
  • DotBPE.RPC是一款基於dotnet core編寫的RPC框架,而它的爸爸DotBPE,目標是實現一個開箱即用的微服務框架,但是它還差點意思,還僅僅在構思和嘗試的階段。但不管怎麼說RPC是微服務的基礎,先來講講RPC的實現吧。DotBPE.RPC底層通信預設實現基於[DotNetty](htt... ...
  • 工廠方法模式(Factory Method) 工廠方法屬於創建型模式中的一種,用於在不指定待創建對象的具體類的情況下創建對象,隱藏了對象創建的複雜性。客戶面向介面或抽象類進行編碼,而Factory類負責具體類的創建。通常,Factory類有一個返回介面或抽象類的靜態方法,客戶提供某種信息,然後由根據 ...
  • 主要介紹ASP.NETMVC 應用提速的六種方法,因為沒有人喜歡等待,所以介紹幾種常用的優化方法。 大家可能會遇到排隊等待,遇到紅燈要等待,開個網頁要等待,等等等。 理所當然,沒有人喜歡等待網頁慢吞吞地載入,尤其是在移動端訪問網站時。其實,Web 開發者敏感的神經決定了我們等待與否。 現在,快速響應 ...
  • ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...