這一章MOMO帶大家討論一下Unity3D中使用的腳本,腳本的最大特點就是用少量的代碼實現(xiàn)繁多的功能,避免大量的代碼。Untiy3D這一塊可以使用腳本做很多東西,那么我們開始學習腳本吧。
這一章MOMO帶大家討論一下Unity3D中使用的腳本,腳本的***特點就是用少量的代碼實現(xiàn)繁多的功能,避免大量的代碼。Untiy3D這一塊可以使用腳本做很多東西,那么我們開始學習腳本吧。
有關(guān)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.
大概意思是介紹三個重要的腳本函數(shù)
Update:這個函數(shù)在渲染幀之前被調(diào)用,大部分的游戲行為代碼都在這里執(zhí)行,除 物理代碼。
FixedUpdate:這個函數(shù)在每進行一次物理時間步調(diào)時被調(diào)用,它是基于物理的游戲行為。
Code outside any function:這類函數(shù)在對象加載時被調(diào)用,它可以用來腳本的初始化工作。
本章我們著重討論Update 這個函數(shù),創(chuàng)建腳本與綁定腳本的方法在第二章中已經(jīng)介紹過了不會的盆友請去那里閱讀。雖然官方推薦腳本使用JavaScript編輯,但是其實C#更符合 Unity3D的編程思想,推薦新人先使用JavaScript,然后在學習C#,因為JavaScript更容易上手一些。

在三維世界中創(chuàng)建兩個矩形,然后在添加兩個腳本分別綁定在這兩個箱子上,腳本的名稱暫時命名為 js0 、js1。
在Project 頁面中打開剛剛創(chuàng)建的js0,發(fā)現(xiàn)Unity3D 已經(jīng)將Update 函數(shù)添加在腳本中了。
模型的移動
Translate方法中的三個參數(shù)分別標示,模型在三維世界中X 、Y、Z 軸移動的單位距離。
[代碼]c#/cpp/oc代碼:
04 |
transform.Translate(1,0,0); |
07 |
transform.Translate(0,1,0); |
10 |
transform.Translate(0,0,1); |
執(zhí)行代碼發(fā)現(xiàn)參數(shù)為1速度居然移動的著么快,怎么能修改移動的速度呢?
Time.deltaTime:標示上一次調(diào)用Update一秒為標示每幀執(zhí)行所消耗的時間。
有了這個參數(shù),我們就可以根據(jù)它修改方向移動的速度了。
[代碼]c#/cpp/oc代碼:
04 |
var translation : float = Time.deltaTime * 10; |
07 |
transform.Translate (translation, 0, 0); |
08 |
transform.Translate (0, translation, 0); |
09 |
transform.Translate (0, 0, translation); |
模型的平移可以選擇一個參照物,下面代碼第二個參數(shù)設(shè)置模型移動參照物,這里設(shè)置成攝像機。那么模型將以相對與攝像機進行移動。
[代碼]c#/cpp/oc代碼:
04 |
var translation : float = Time.deltaTime * 10; |
07 |
transform.Translate(Vector3.right * translation, Camera.main.transform); |
10 |
transform.Translate(Vector3.up * translation, Camera.main.transform); |
13 |
transform.Translate(Vector3.left * translation, Camera.main.transform); |
模型的旋轉(zhuǎn)
Rotate方法中的三個參數(shù)分別標示,模型在三維世界中X 、Y、Z 軸旋轉(zhuǎn)的單位距離。
[代碼]c#/cpp/oc代碼:
03 |
//以模型X軸旋轉(zhuǎn),單位為2. |
04 |
transform.Rotate(2, 0, 0); |
06 |
//以模型Y軸旋轉(zhuǎn),單位為2. |
07 |
transform.Rotate(0, 2, 0); |
09 |
//以模型Z軸旋轉(zhuǎn),單位為2. |
10 |
transform.Rotate(0, 0, 2); |
模型的旋轉(zhuǎn)可以選擇一個參照物,下面代碼第二個參數(shù)設(shè)置模型移動參照物,這里設(shè)置成3D世界。那么模型將以相對與整個3D世界進行旋轉(zhuǎn)。
[代碼]c#/cpp/oc代碼:
04 |
var rotate : float = Time.deltaTime * 100; |
08 |
//相對于世界坐標中心向右旋轉(zhuǎn)物體 |
09 |
transform.Rotate(Vector3.right * rotate, Space.World); |
11 |
//相對于世界坐標中心向上旋轉(zhuǎn)物體 |
12 |
transform.Rotate(Vector3.up * rotate, Space.World); |
14 |
//相對于世界坐標中心向左旋轉(zhuǎn)物體 |
15 |
transform.Rotate(Vector3.left * rotate, Space.World); |
如下圖所示,給出一個小例子,在腳本中移動箱子的坐標,在屏幕中記錄模型移動的位置,并且顯示在游戲視圖中。效果很不錯吧,嘻嘻~~
完整代碼
[代碼]c#/cpp/oc代碼:
13 |
var x : float = Time.deltaTime * 10; |
14 |
var y : float = Time.deltaTime * 8; |
15 |
var z : float = Time.deltaTime * 5; |
18 |
transform.Translate (x, 0, 0); |
21 |
transform.Translate (0, y, 0); |
23 |
transform.Translate (0, 0, z); |
35 |
GUI.Label(Rect(50, 100,200,20),"x pos is" + posX +"float"); |
36 |
GUI.Label(Rect(50, 120,200,20),"y pos is" + posY +"float"); |
37 |
GUI.Label(Rect(50, 140,200,20),"z pos is" + posZ +"float"); |