Unity3D 遊戲引擎之指令碼實現模型的平移與旋轉(六)

來源:互聯網
上載者:User
Unity3D 遊戲引擎之指令碼實現模型的平移與旋轉


雨松MOMO原創文章如轉載,請註明:轉載至我的獨立網域名稱部落格雨松MOMO程式研究院 ,原文地址:http://www.xuanyusong.com/archives/501



這一章MOMO帶大家討論一下Unity3D中使用的指令碼,指令碼的最大特點就是用少量的代碼實現繁多的功能,避免大量的代碼。Untiy3D這一塊可以使用指令碼做很多東西,那麼我們開始學習指令碼吧。



有關Unity3D 指令碼的API所有文檔盆友們都可以去這裡查閱。
官方API 文檔:http://unity3d.com/support/documentation/ScriptReference/

指令碼描述

Scripting inside Unity consists of attaching custom script objects called behaviours to game objects. Different functions inside the script objects are called on certain events. The most used ones being the following:

Update:
This function is called before rendering a frame. This is where most game behaviour code goes, except physics code.
FixedUpdate:
This function is called once every physics time step. This is the place to do physics-based game behaviour.
Code outside any function:
Code outside functions is run when the object is loaded. This can be used to initialise the state of the script.
Note: Sections of this document assume you are using Javascript, but see Writing scripts in C# & Boo for information about how to use C# or Boo scripts.

大概意思是介紹三個重要的指令碼函數

Update:這個函數在渲染幀之前被調用,大部分的遊戲行為代碼都在這裡執行,除 物理代碼。

FixedUpdate:這個函數在每進行一次物理時間步調時被調用,它是基於物理的遊戲行為。

Code outside any function:這類函數在對象載入時被調用,它可以用來指令碼的初始化工作。




本章我們著重討論Update 這個函數,建立指令碼與綁定指令碼的方法在第二章中已經介紹過了不會的盆友請去那裡閱讀。雖然官方推薦指令碼使用JavaScript編輯,但是其實C#更符合Unity3D的編程思想,推薦新人先使用JavaScript,然後在學習C#,因為JavaScript更容易上手一些。











在三維世界中建立兩個矩形,然後在添加兩個指令碼分別綁定在這兩個箱子上,指令碼的名稱暫時命名為 js0 、js1。

在Project 頁面中開啟剛剛建立的js0,發現Unity3D 已經將Update 函數添加在指令碼中了。

模型的移動

Translate方法中的三個參數分別標示,模型在三維世界中X 、Y、Z 軸移動的單位距離。

function Update () {//模型x軸,移動一個單位transform.Translate(1,0,0);//模型y軸,移動一個單位transform.Translate(0,1,0);//模型z軸,移動一個單位transform.Translate(0,0,1);}

執行代碼發現參數為1速度居然移動的著麼快,怎麼能修改移動的速度呢?


Time.deltaTime:標示上一次調用Update一秒為標示每幀執行所消耗的時間。

有了這個參數,我們就可以根據它修改方向移動的速度了。

function Update () {    //設定移動的範圍    var translation : float = Time.deltaTime * 10;        //移動的方向    transform.Translate (translation, 0, 0);    transform.Translate (0, translation, 0);    transform.Translate (0, 0, translation);}

模型的平移可以選擇一個參照物,下面代碼第二個參數設定模型移動參照物,這裡設定成攝像機。那麼模型將以相對與攝像機進行移動。

function Update () {    //設定移動範圍    var translation : float = Time.deltaTime * 10;    //相對於攝像機,模型向右移動。transform.Translate(Vector3.right * translation, Camera.main.transform);// 相對於攝像機,模型向上移動。transform.Translate(Vector3.up * translation, Camera.main.transform);// 相對於攝像機,模型向左移動。transform.Translate(Vector3.left * translation, Camera.main.transform);}

模型的旋轉


Rotate方法中的三個參數分別標示,模型在三維世界中X 、Y、Z 軸旋轉的單位距離。


function Update () {      //以模型X軸旋轉,單位為2.   transform.Rotate(2, 0, 0);      //以模型Y軸旋轉,單位為2.   transform.Rotate(0, 2, 0);   //以模型Z軸旋轉,單位為2.   transform.Rotate(0, 0, 2);}


模型的旋轉可以選擇一個參照物,下面代碼第二個參數設定模型移動參照物,這裡設定成3D世界。那麼模型將以相對與整個3D世界進行旋轉。



function Update () {      //設定旋轉的範圍    var rotate : float = Time.deltaTime * 100;        //旋轉的方向        //相對於全局座標中心向右旋轉物體    transform.Rotate(Vector3.right * rotate, Space.World);         //相對於全局座標中心向上旋轉物體    transform.Rotate(Vector3.up * rotate, Space.World);         //相對於全局座標中心向左旋轉物體    transform.Rotate(Vector3.left * rotate, Space.World);}



如所示,給出一個小例子,在指令碼中移動箱子的座標,在螢幕中記錄模型移動的位置,並且顯示在遊戲視圖中。效果很不錯吧,嘻嘻~~




完整代碼



//X軸移動位置var posX : float;//Y軸移動位置var posY : float;//Z軸移動位置var posZ : float;function Update () {     //設定移動的範圍    var x : float = Time.deltaTime * 10;    var y : float = Time.deltaTime * 8;    var z : float = Time.deltaTime * 5;        //移動的方向X軸    transform.Translate (x, 0, 0);        //移動的方向Y軸    transform.Translate (0, y, 0);    //移動的方向Z軸    transform.Translate (0, 0, z);            //賦值計算模型在三維座標系中的位置     posX += x;      posY += y;      posZ += z; }function OnGUI () {              //將座標資訊顯示在3D螢幕中  GUI.Label(Rect(50, 100,200,20),"x pos is" + posX +"float");    GUI.Label(Rect(50, 120,200,20),"y pos is" + posY +"float");    GUI.Label(Rect(50, 140,200,20),"z pos is" + posZ +"float");    }  



Unity3D 的世界中指令碼還可以做很多事情,以後我在慢慢向各位道來~ 歡迎各位盆友可以和MOMO一起討論Unity3D遊戲開發,哇哢哢~~~



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.