转载自:http://blog.csdn.net/janpylx/article/details/7770766
这里就有点杂了,因为很多东西需要配合着项目才好解释。所以以下为我对一些函数(独立出来了)的理解。嘿嘿,一个大合集哦,可能下周还要继续写。
transform.Translate--把对象从一个地方往给定的方向前进。
例如:
transform.Translate(<a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Vector3-forward.html?from=Transform" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">Vector3.forward</a> * <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Time-deltaTime.html?from=Transform" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">Time.deltaTime</a>);<span style="white-space: pre;"> </span>// Vector3.forward相当于(0, 0, 1). Time.deltaTime见下面。
transform.position--获取对象的坐标(x, y, z)。
例如:
transform.position = <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Vector3.html" class="classlink" style="color: rgb(20, 93, 123); text-decoration: none;">Vector3</a>(0, 0, 0);<span style="white-space: pre;"> </span>// 把对象移动到(0, 0, 0)。
gameObject.GetComponent--获得,怎么说呢,类似于返回一个指向gameObject(对象)的一个组件的指针吧。
例如:
curTransform = gameObject.GetComponent(<a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Transform.html" class="classlink" style="color: rgb(20, 93, 123); text-decoration: none;">Transform</a>);<span style="white-space: pre;"> </span>// curTransform一个变量,后面的语句返回Transform这个组件。
GameObject.FindWithTag --根据gameObject(对象)的标签来寻找对象。如果找到的话,就返回一个指向该对象的指针。
例如:
<span class="hl-keyword">var</span> respawn = <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindWithTag.html" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">GameObject.FindWithTag</a> (<span class="hl-string">"Respawn"</span>);<span style="white-space: pre;"> </span>// var respawn是在定义一个新的变量respawn,后面的语句将返回一个指向标签为Raspawn的对象的指针。
GameObject.FindGameObjectsWithTag--根据gameObject(对象)的标签来寻找一组具有相同标签的对象。如果找到的话,就返回一个指向该对象数组的指针。
例如:
<span class="hl-keyword">var</span> respawns = <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">GameObject.FindGameObjectsWithTag</a> (<span class="hl-string">"Respawn"</span>);<span style="white-space: pre;"> </span><span style="font-size: 8pt;">// var respawn是在定义一个新的变量respawn,</span><span style="font-size: 8pt;">后面的语句将返回一个指向标签为Raspawn的一组对象的指针。</span>
Input.GetKeyDown和Input.GetButtonDown --这两者类似,当用户按下某个特定的键后,触发事件。
例如:
<span class="hl-keyword">function</span> <span class="hl-operator">Update</span> () { <span class="hl-keyword">if</span> (<a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Input.GetKeyDown.html" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">Input.GetKeyDown</a> (<span class="hl-string">"space"</span>)) print (<span class="hl-string">"space key was pressed"</span>); }<span style="white-space: pre;"> </span>// 如果用户按下space键(空格键),则打印<span style="font-size: 8pt;">space key was pressed。<a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButtonDown.html" target="_blank" style="color: rgb(51, 102, 153); text-decoration: none;">Input.GetButtonDown</a>类似。</span>
Input.GetAxis和Input.GetAxisRaw --以名字来辨别虚拟键盘上的标识符。两者功能类似。后者限制更少。
例如:
<span class="hl-keyword">var</span> speed : <span class="hl-datatype">float</span> = <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Input.GetAxisRaw.html" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">Input.GetAxisRaw</a>(<span class="hl-string">"Horizontal"</span>) * <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Time-deltaTime.html?from=Input" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">Time.deltaTime</a>;<span style="white-space: pre;"> </span>// 接收左右或者A、D两个键的输入,控制水平方向的移动。Time.time --从游戏开始算起的总时间。
Time.deltaTime --每一秒的最后一帧所用的时间。
Object.Instantiate --克隆对象。
例如:
Instantiate (prefab, <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Vector3.html" class="classlink" style="color: rgb(20, 93, 123); text-decoration: none;">Vector3</a>(i * 2.0, 0, 0), <a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Quaternion-identity.html?from=Object" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">Quaternion.identity</a>);<span style="white-space: pre;"> </span>// prefab是被克隆的对象, 第二个变量是克隆体生成的位置,第三个变量表示该生成的克隆不旋转。
function OnCollisionEnter(collision : Collision) --检测碰撞,且碰撞的双方有一方为刚体。例子见官方文档。
function OnTriggerEnter (other : Collider) --检测碰撞,且碰撞的双方有一方为刚体(另外,被装的一方还要在开启is trigger属性)。例子见官方文档。
Destroy --摧毁对象。
例如:
Destroy (gameObject, 5);<span style="white-space: pre;"> </span>// 5秒后销毁gameObject。Vector3 --代表一个三维坐标。
Random.Range --在一定范围内获得随机数。
例如:
<a target=_blank href="http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html" class="itemlink" style="color: rgb(20, 93, 123); text-decoration: none;">Random.Range</a>(-10.0, 10.0)<span style="white-space: pre;"> </span>// 获得-10.0到10.0的随机数。