Unity3D 游戏引擎之脚本实现模型的平移与旋转

移动开发 iOS 游戏开发
这一章MOMO带大家讨论一下Unity3D中使用的脚本,脚本的最大特点就是用少量的代码实现繁多的功能,避免大量的代码。Untiy3D这一块可以使用脚本做很多东西,那么我们开始学习脚本吧。
这一章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 轴移动的单位距离。

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

01 function Update () { 
02    
03 //模型x轴,移动一个单位 
04 transform.Translate(1,0,0); 
05    
06 //模型y轴,移动一个单位 
07 transform.Translate(0,1,0); 
08    
09 //模型z轴,移动一个单位 
10 transform.Translate(0,0,1); 
11    
12 }

执行代码发现参数为1速度居然移动的着么快,怎么能修改移动的速度呢?

Time.deltaTime:标示上一次调用Update一秒为标示每帧执行所消耗的时间。

有了这个参数,我们就可以根据它修改方向移动的速度了。

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

01 function Update () { 
02    
03     //设置移动的范围 
04     var translation : float = Time.deltaTime * 10; 
05        
06     //移动的方向 
07     transform.Translate (translation, 0, 0); 
08     transform.Translate (0, translation, 0); 
09     transform.Translate (0, 0, translation); 
10    
11 }

模型的平移可以选择一个参照物,下面代码第二个参数设置模型移动参照物,这里设置成摄像机。那么模型将以相对与摄像机进行移动。

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

01 function Update () { 
02    
03     //设置移动范围 
04     var translation : float = Time.deltaTime * 10; 
05        
06     //相对于摄像机,模型向右移动。 
07     transform.Translate(Vector3.right * translation, Camera.main.transform); 
08        
09     // 相对于摄像机,模型向上移动。 
10     transform.Translate(Vector3.up * translation, Camera.main.transform); 
11        
12     // 相对于摄像机,模型向左移动。 
13     transform.Translate(Vector3.left * translation, Camera.main.transform); 
14        
15     }

模型的旋转

 

Rotate方法中的三个参数分别标示,模型在三维世界中X 、Y、Z 轴旋转的单位距离。

 

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

01 function Update () { 
02       
03    //以模型X轴旋转,单位为2. 
04    transform.Rotate(2, 0, 0); 
05       
06    //以模型Y轴旋转,单位为2. 
07    transform.Rotate(0, 2, 0); 
08        
09    //以模型Z轴旋转,单位为2. 
10    transform.Rotate(0, 0, 2); 
11 }
模型的旋转可以选择一个参照物,下面代码第二个参数设置模型移动参照物,这里设置成3D世界。那么模型将以相对与整个3D世界进行旋转。

 

 

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

01 function Update () { 
02       
03    //设置旋转的范围 
04     var rotate : float = Time.deltaTime * 100; 
05        
06     //旋转的方向 
07        
08     //相对于世界坐标中心向右旋转物体 
09     transform.Rotate(Vector3.right * rotate, Space.World); 
10        
11      //相对于世界坐标中心向上旋转物体 
12     transform.Rotate(Vector3.up * rotate, Space.World); 
13        
14      //相对于世界坐标中心向左旋转物体 
15     transform.Rotate(Vector3.left * rotate, Space.World); 
16 }
如下图所示,给出一个小例子,在脚本中移动箱子的坐标,在屏幕中记录模型移动的位置,并且显示在游戏视图中。效果很不错吧,嘻嘻~~

完整代码

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

01 //X轴移动位置 
02 var posX : float; 
03 //Y轴移动位置 
04 var posY : float; 
05 //Z轴移动位置 
06 var posZ : float; 
07    
08    
09    
10 function Update () { 
11       
12   //设置移动的范围 
13     var x : float = Time.deltaTime * 10; 
14     var y : float = Time.deltaTime * 8; 
15     var z : float = Time.deltaTime * 5; 
16        
17     //移动的方向X轴 
18     transform.Translate (x, 0, 0); 
19        
20     //移动的方向Y轴 
21     transform.Translate (0, y, 0); 
22     //移动的方向Z轴 
23     transform.Translate (0, 0, z); 
24        
25        
26     //赋值计算模型在三维坐标系中的位置 
27      posX += x;  
28      posY += y;  
29      posZ += z;  
30
31    
32 function OnGUI () {   
33              
34   //将坐标信息显示在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 }
责任编辑:冰凝儿
相关推荐

2012-12-24 08:52:44

iOSUnity3D

2012-12-24 08:46:50

iOSUnity3D

2012-12-24 08:54:47

iOSUnity3D

2012-12-24 08:40:12

2012-12-24 09:06:14

iOSUnity3D

2012-12-24 08:48:25

iOSUnity3D

2012-12-24 08:45:19

iOSUnity3D

2012-12-24 09:04:04

iOSUnity3D

2012-12-24 08:50:21

iOSUnity3D

2013-04-25 09:56:24

unity3D手机游戏引擎

2012-12-24 08:57:35

iOSUnity3D

2012-12-24 09:01:41

iOSUnity3D

2012-12-24 09:07:09

iOSUnity3D

2012-12-24 09:00:31

iOSUnity3D

2012-12-24 08:59:13

iOSUnity3D

2013-04-25 10:03:07

unity3D手机游戏引擎

2012-12-24 08:56:15

iOSUnity3D

2012-12-24 09:02:48

iOSUnity3D

2012-12-24 09:11:58

iOSUnity3D

2012-12-24 09:20:48

AndoidUnity3D
点赞
收藏

51CTO技术栈公众号