Unity3D實現(xiàn)控制攝像機移動
本文實例為大家分享了Unity3D實現(xiàn)控制攝像機移動的具體代碼,供大家參考,具體內(nèi)容如下
最近公司的幾個項目開發(fā)內(nèi)容基本相同,很多腳本直接復(fù)制過來就可以拼接項目。之前一直是代碼愛好者,能自己敲的絕對不去復(fù)制粘貼。但是開發(fā)速度確實是被耽誤了,所以接下來打算把開發(fā)中常用的腳本都發(fā)到博客上。自己需要的時候直接拿來。也希望能幫到你們。
unity編輯器中按住鼠標右鍵,在通過控制鍵盤的wasdqe鍵可以自由控制視野。
下面就是實現(xiàn)操作的代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
//巡游模式攝像機控制
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);
    }
 
    //移動
    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);
    //限制攝像機范圍
 transform.position = Vector3.ClampMagnitude(transform.position, dis);
  }
}
由于項目需要限制攝像機的移動范圍,所以我在最后加上了限制的代碼。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:C#基于HttpWebRequest實現(xiàn)發(fā)送HTTP請求的方法分析
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4913.html
您可能感興趣的文章
- 01-10C#實現(xiàn)txt定位指定行完整實例
 - 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
 - 01-10C#實現(xiàn)清空回收站的方法
 - 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已安裝軟件變化的方法
 - 01-10C#實現(xiàn)多線程下載文件的方法
 - 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
 - 01-10C#實現(xiàn)遠程關(guān)閉計算機或重啟計算機的方法
 - 01-10C#自定義簽名章實現(xiàn)方法
 - 01-10C#文件斷點續(xù)傳實現(xiàn)方法
 - 01-10winform實現(xiàn)創(chuàng)建最前端窗體的方法
 


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實現(xiàn)txt定位指定行完整實例
 - 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
 - 01-10C#停止線程的方法
 - 01-10C#實現(xiàn)清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
 
隨機閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-11ajax實現(xiàn)頁面的局部加載
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 04-02jquery與jsp,用jquery
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 01-10C#中split用法實例總結(jié)
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-10delphi制作wav文件的方法
 


