自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Unity3D 游戲引擎之腳本實(shí)現(xiàn)模型的平移與旋轉(zhuǎn)

移動(dòng)開發(fā) iOS 游戲開發(fā)
這一章MOMO帶大家討論一下Unity3D中使用的腳本,腳本的最大特點(diǎn)就是用少量的代碼實(shí)現(xiàn)繁多的功能,避免大量的代碼。Untiy3D這一塊可以使用腳本做很多東西,那么我們開始學(xué)習(xí)腳本吧。
這一章MOMO帶大家討論一下Unity3D中使用的腳本,腳本的***特點(diǎn)就是用少量的代碼實(shí)現(xiàn)繁多的功能,避免大量的代碼。Untiy3D這一塊可以使用腳本做很多東西,那么我們開始學(xué)習(xí)腳本吧。
有關(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.
大概意思是介紹三個(gè)重要的腳本函數(shù)

Update:這個(gè)函數(shù)在渲染幀之前被調(diào)用,大部分的游戲行為代碼都在這里執(zhí)行,除 物理代碼。

FixedUpdate:這個(gè)函數(shù)在每進(jìn)行一次物理時(shí)間步調(diào)時(shí)被調(diào)用,它是基于物理的游戲行為。

Code outside any function:這類函數(shù)在對(duì)象加載時(shí)被調(diào)用,它可以用來腳本的初始化工作。

本章我們著重討論Update 這個(gè)函數(shù),創(chuàng)建腳本與綁定腳本的方法在第二章中已經(jīng)介紹過了不會(huì)的盆友請(qǐng)去那里閱讀。雖然官方推薦腳本使用JavaScript編輯,但是其實(shí)C#更符合 Unity3D的編程思想,推薦新人先使用JavaScript,然后在學(xué)習(xí)C#,因?yàn)镴avaScript更容易上手一些。

在三維世界中創(chuàng)建兩個(gè)矩形,然后在添加兩個(gè)腳本分別綁定在這兩個(gè)箱子上,腳本的名稱暫時(shí)命名為 js0 、js1。

在Project 頁面中打開剛剛創(chuàng)建的js0,發(fā)現(xiàn)Unity3D 已經(jīng)將Update 函數(shù)添加在腳本中了。

模型的移動(dòng)

Translate方法中的三個(gè)參數(shù)分別標(biāo)示,模型在三維世界中X 、Y、Z 軸移動(dòng)的單位距離。

[代碼]c#/cpp/oc代碼:

01 function Update () { 
02    
03 //模型x軸,移動(dòng)一個(gè)單位 
04 transform.Translate(1,0,0); 
05    
06 //模型y軸,移動(dòng)一個(gè)單位 
07 transform.Translate(0,1,0); 
08    
09 //模型z軸,移動(dòng)一個(gè)單位 
10 transform.Translate(0,0,1); 
11    
12 }

執(zhí)行代碼發(fā)現(xiàn)參數(shù)為1速度居然移動(dòng)的著么快,怎么能修改移動(dòng)的速度呢?

Time.deltaTime:標(biāo)示上一次調(diào)用Update一秒為標(biāo)示每幀執(zhí)行所消耗的時(shí)間。

有了這個(gè)參數(shù),我們就可以根據(jù)它修改方向移動(dòng)的速度了。

[代碼]c#/cpp/oc代碼:

01 function Update () { 
02    
03     //設(shè)置移動(dòng)的范圍 
04     var translation : float = Time.deltaTime * 10; 
05        
06     //移動(dòng)的方向 
07     transform.Translate (translation, 0, 0); 
08     transform.Translate (0, translation, 0); 
09     transform.Translate (0, 0, translation); 
10    
11 }

模型的平移可以選擇一個(gè)參照物,下面代碼第二個(gè)參數(shù)設(shè)置模型移動(dòng)參照物,這里設(shè)置成攝像機(jī)。那么模型將以相對(duì)與攝像機(jī)進(jìn)行移動(dòng)。

[代碼]c#/cpp/oc代碼:

01 function Update () { 
02    
03     //設(shè)置移動(dòng)范圍 
04     var translation : float = Time.deltaTime * 10; 
05        
06     //相對(duì)于攝像機(jī),模型向右移動(dòng)。 
07     transform.Translate(Vector3.right * translation, Camera.main.transform); 
08        
09     // 相對(duì)于攝像機(jī),模型向上移動(dòng)。 
10     transform.Translate(Vector3.up * translation, Camera.main.transform); 
11        
12     // 相對(duì)于攝像機(jī),模型向左移動(dòng)。 
13     transform.Translate(Vector3.left * translation, Camera.main.transform); 
14        
15     }

模型的旋轉(zhuǎn)

 

Rotate方法中的三個(gè)參數(shù)分別標(biāo)示,模型在三維世界中X 、Y、Z 軸旋轉(zhuǎn)的單位距離。

 

[代碼]c#/cpp/oc代碼:

01 function Update () { 
02       
03    //以模型X軸旋轉(zhuǎn),單位為2. 
04    transform.Rotate(2, 0, 0); 
05       
06    //以模型Y軸旋轉(zhuǎn),單位為2. 
07    transform.Rotate(0, 2, 0); 
08        
09    //以模型Z軸旋轉(zhuǎn),單位為2. 
10    transform.Rotate(0, 0, 2); 
11 }
模型的旋轉(zhuǎn)可以選擇一個(gè)參照物,下面代碼第二個(gè)參數(shù)設(shè)置模型移動(dòng)參照物,這里設(shè)置成3D世界。那么模型將以相對(duì)與整個(gè)3D世界進(jìn)行旋轉(zhuǎn)。

 

 

[代碼]c#/cpp/oc代碼:

01 function Update () { 
02       
03    //設(shè)置旋轉(zhuǎn)的范圍 
04     var rotate : float = Time.deltaTime * 100; 
05        
06     //旋轉(zhuǎn)的方向 
07        
08     //相對(duì)于世界坐標(biāo)中心向右旋轉(zhuǎn)物體 
09     transform.Rotate(Vector3.right * rotate, Space.World); 
10        
11      //相對(duì)于世界坐標(biāo)中心向上旋轉(zhuǎn)物體 
12     transform.Rotate(Vector3.up * rotate, Space.World); 
13        
14      //相對(duì)于世界坐標(biāo)中心向左旋轉(zhuǎn)物體 
15     transform.Rotate(Vector3.left * rotate, Space.World); 
16 }
如下圖所示,給出一個(gè)小例子,在腳本中移動(dòng)箱子的坐標(biāo),在屏幕中記錄模型移動(dòng)的位置,并且顯示在游戲視圖中。效果很不錯(cuò)吧,嘻嘻~~

完整代碼

[代碼]c#/cpp/oc代碼:

01 //X軸移動(dòng)位置 
02 var posX : float; 
03 //Y軸移動(dòng)位置 
04 var posY : float; 
05 //Z軸移動(dòng)位置 
06 var posZ : float; 
07    
08    
09    
10 function Update () { 
11       
12   //設(shè)置移動(dòng)的范圍 
13     var x : float = Time.deltaTime * 10; 
14     var y : float = Time.deltaTime * 8; 
15     var z : float = Time.deltaTime * 5; 
16        
17     //移動(dòng)的方向X軸 
18     transform.Translate (x, 0, 0); 
19        
20     //移動(dòng)的方向Y軸 
21     transform.Translate (0, y, 0); 
22     //移動(dòng)的方向Z軸 
23     transform.Translate (0, 0, z); 
24        
25        
26     //賦值計(jì)算模型在三維坐標(biāo)系中的位置 
27      posX += x;  
28      posY += y;  
29      posZ += z;  
30
31    
32 function OnGUI () {   
33              
34   //將坐標(biāo)信息顯示在3D屏幕中 
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");   
38      
39 }
責(zé)任編輯:冰凝兒
相關(guān)推薦

2012-12-24 08:52:44

iOSUnity3D

2012-12-24 08:46:50

iOSUnity3D

2012-12-24 08:40:12

2012-12-24 09:06:14

iOSUnity3D

2012-12-24 08:54:47

iOSUnity3D

2012-12-24 08:48:25

iOSUnity3D

2012-12-24 08:45:19

iOSUnity3D

2012-12-24 08:57:35

iOSUnity3D

2012-12-24 09:01:41

iOSUnity3D

2012-12-24 08:50:21

iOSUnity3D

2012-12-24 09:04:04

iOSUnity3D

2013-04-25 09:56:24

unity3D手機(jī)游戲引擎

2012-12-24 09:07:09

iOSUnity3D

2012-12-24 09:00:31

iOSUnity3D

2012-12-24 08:59:13

iOSUnity3D

2012-12-24 09:02:48

iOSUnity3D

2013-04-25 10:03:07

unity3D手機(jī)游戲引擎

2012-12-24 08:56:15

iOSUnity3D

2012-12-24 09:11:58

iOSUnity3D

2012-12-24 09:20:48

AndoidUnity3D
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)