Unity3D實(shí)現(xiàn)控制攝像機(jī)移動(dòng)
本文實(shí)例為大家分享了Unity3D實(shí)現(xiàn)控制攝像機(jī)移動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
最近公司的幾個(gè)項(xiàng)目開發(fā)內(nèi)容基本相同,很多腳本直接復(fù)制過來就可以拼接項(xiàng)目。之前一直是代碼愛好者,能自己敲的絕對(duì)不去復(fù)制粘貼。但是開發(fā)速度確實(shí)是被耽誤了,所以接下來打算把開發(fā)中常用的腳本都發(fā)到博客上。自己需要的時(shí)候直接拿來。也希望能幫到你們。
unity編輯器中按住鼠標(biāo)右鍵,在通過控制鍵盤的wasdqe鍵可以自由控制視野。
下面就是實(shí)現(xiàn)操作的代碼:
using System.Collections; using System.Collections.Generic; using UnityEngine; //巡游模式攝像機(jī)控制 public class CameraMove : MonoBehaviour { public static CameraMove Instance = null; private Vector3 dirVector3; private Vector3 rotaVector3; private float paramater = 0.1f; //旋轉(zhuǎn)參數(shù) private float xspeed = -0.05f; private float yspeed = 0.1f; private float dis; void Awake () { Instance = this; } private void Start() { rotaVector3 = transform.localEulerAngles; dis = UIFuc.Instance.Dis; } // Update is called once per frame void FixedUpdate () { //旋轉(zhuǎn) if (Input.GetMouseButton(1)) { rotaVector3.y += Input.GetAxis("Horizontal") * yspeed; rotaVector3.x += Input.GetAxis("Vertical") * xspeed; transform.rotation = Quaternion.Euler(rotaVector3); } //移動(dòng) dirVector3 =Vector3.zero; if (Input.GetKey(KeyCode.W)) { if(Input.GetKey(KeyCode.LeftShift)) dirVector3.z = 3; else dirVector3.z = 1; } if (Input.GetKey(KeyCode.S)) { if (Input.GetKey(KeyCode.LeftShift)) dirVector3.z = -3; else dirVector3.z = -1; } if (Input.GetKey(KeyCode.A)) { if (Input.GetKey(KeyCode.LeftShift)) dirVector3.x = -3; else dirVector3.x = -1; } if (Input.GetKey(KeyCode.D)) { if (Input.GetKey(KeyCode.LeftShift)) dirVector3.x = 3; else dirVector3.x = 1; } if (Input.GetKey(KeyCode.Q)) { if (Input.GetKey(KeyCode.LeftShift)) dirVector3.y = -3; else dirVector3.y = -1; } if (Input.GetKey(KeyCode.E)) { if (Input.GetKey(KeyCode.LeftShift)) dirVector3.y = 3; else dirVector3.y = 1; } transform.Translate(dirVector3 * paramater,Space.Self); //限制攝像機(jī)范圍 transform.position = Vector3.ClampMagnitude(transform.position, dis); } }
由于項(xiàng)目需要限制攝像機(jī)的移動(dòng)范圍,所以我在最后加上了限制的代碼。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Unity3D實(shí)現(xiàn)攝像機(jī)鏡頭移動(dòng)并限制角度
- Unity鍵盤WASD實(shí)現(xiàn)物體移動(dòng)
- Unity3D實(shí)現(xiàn)虛擬按鈕控制人物移動(dòng)效果
- Unity3D實(shí)現(xiàn)物體旋轉(zhuǎn)縮放移動(dòng)效果
- Unity實(shí)現(xiàn)物體左右移動(dòng)效果
- Unity3D實(shí)現(xiàn)人物轉(zhuǎn)向與移動(dòng)
- Unity3D實(shí)現(xiàn)人物移動(dòng)示例
- unity實(shí)現(xiàn)UI元素跟隨3D物體
- unity實(shí)現(xiàn)攝像頭跟隨
- Unity實(shí)現(xiàn)主角移動(dòng)與攝像機(jī)跟隨
相關(guān)文章
基于c#實(shí)現(xiàn)的九九乘法表(簡單實(shí)例)
本文主要分享了基于c#實(shí)現(xiàn)的九九乘法表,代碼簡潔,需要的朋友可以參考下,希望對(duì)大家有所幫助2016-12-12詳解WPF如何在基礎(chǔ)控件上顯示Loading等待動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了WPF如何在基礎(chǔ)控件上顯示Loading等待動(dòng)畫的效果,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下2023-04-04Js中的substring,substr與C#中的Substring比較
本篇文章主要是對(duì)Js中的substring,substr與C#中的Substring進(jìn)行了比較。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01C#?如何使用批處理啟動(dòng)Windows服務(wù)
這篇文章主要介紹了C#使用批處理啟動(dòng)Windows服務(wù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06C#環(huán)形隊(duì)列的實(shí)現(xiàn)方法詳解
這篇文章先是簡單的給大家介紹了什么是環(huán)形隊(duì)列和環(huán)形隊(duì)列的優(yōu)點(diǎn),然后通過實(shí)例代碼給大家介紹C#如何實(shí)現(xiàn)環(huán)形隊(duì)列,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-09-09C#之Windows自帶打印功能的實(shí)現(xiàn)
這篇文章主要介紹了C#之Windows自帶打印功能的實(shí)現(xiàn)方式,具有很好的價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06【C#基礎(chǔ)】Substring截取字符串的方法小結(jié)(推薦)
這篇文章主要介紹了Substring截取字符串方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05C#基礎(chǔ)語法:as 運(yùn)算符使用實(shí)例
這篇文章主要介紹了C#基礎(chǔ)語法:as 運(yùn)算符使用實(shí)例,本文給出了類、字符串、數(shù)字、浮點(diǎn)數(shù)、null等值的運(yùn)算實(shí)例,需要的朋友可以參考下2015-06-06