1. 程式人生 > >【U3D入門小白教程——代碼篇】之三:常見腳本函數

【U3D入門小白教程——代碼篇】之三:常見腳本函數

force avi log tar 啟動 for gravity vector 教程

個人學習第三章節:常見腳本

1.Awake( )與Start()

Awake( )在遊戲創建時調用,用於設置遊戲初始化的參數

Start( )在腳本被調用,執行在所有Update( )之前,用於賦值變量

2.Update( )與FixedUpdate()

Update( )用於非剛體對象的運動,諸如定時器,檢測輸入等,Update受制於機器性能幀率

FixedUpdate()用於剛體對象運動,不受制於機器性能,有固定調用間隔。

3.Vector3.Dot( )與Vector3.Cross( )

Vector3.Dot( )用於表示兩個向量的乘積,乘積為-1則為反方向,為0則兩個向量垂直

Vector3.Cross( )用於計算兩個向量交叉乘積,乘積結果為垂直於兩個向量的新的向量

4.transform.Translate( )與transform.Rotate( )

transform.Translate( )用於指定向量的位移

transform.Rotate( )用於選定向量為旋轉中心(視線為向量方向)的逆時針旋轉

這裏z軸正向為transform.forward 負向為transform.back

y軸正向為transform.up 負向為transform.down

x軸正向為transform.left 負向為transform.right

5.transform.LookAt(target)

transform.LookAt(target)用於指定物體z軸始終指向target,用於camera類似於第一人稱視角

6.Math.Lerp( )

舉例:float result = Mathf.Lerp (3f, 5f, 0.5f); 得到結果為4

Vector3 from = new Vector3 (1f, 2f, 3f);

Vector3 to = new Vector3 (5f, 6f, 7f);

Vector3 result = Vector3.Lerp (from, to, 0.75f); 得到結果為(4,5,6)

補充SmoothDamp( currentPosition ,targetPosition ,ref Velocity ,smoothTime )

currentPosition:我們正在的位置

targetPosition:目標移動位置

ref Velocity:不用管,Vector3 Velocity = Vector3.zero;

smoothTime:移動所要時間 註意這裏smoothTime = 3f 並不為 3s(感覺大概是)

用於隨著時間的推移,逐漸將向量改變為所需的目標。

7.Destory( )

用於gameobject或者compoent的清除

Destory(target,3f) 目標將在3s後摧毀

8.Input.GetButton( )與Input.GetKey( )

當按下按鍵,GetButtonDown( )僅按下觸發一次為True,其他為False

GetButtonUp( )僅談起觸發一次為True,其他為False

GetButton( )只要按鍵按下未彈起,即為True

GetKey( ) GetKeyDown( ) GetKeyUp( )用法一樣

Input.GetKey( )設定按鍵在Unity窗口Edit/Project Settings/Input中

9.GetAxis( )

GetAxis( )類似於GetKey( ),設定按鍵在Unity窗口Edit/Project Settings/Input中

主要用法GetAxis(“Vertical”),GetAxis(“Horizontal”)

按下方向鍵正向,GetAxis( )數值從0到1增加,然後松開返回值為0

在Input設置中修改Gravity大小,決定了換向時候變化的速度,數值越大速度越快

修改Sensitivity大小,決定了按鍵從數值變化的快慢,數值越大速度越快

GetAxisRaw( )該值的範圍為-1 ... 1,當GetAxis處理物體移動有明顯跳幀問題,則使用此方法平滑移動

10.OnMouseDown( )

OnMouseDown( )用於鼠標點擊事件

其他鼠標事件還有

OnMouseEnter( )當鼠標移動到對象上觸發

OnMouseOver( )當鼠標停在對象上觸發

OnMouseExit( )當鼠標移動到對象後離開觸發

OnMouseDrag( )當鼠標到對象上按住觸發

11.GetComponent<>( )

可以用這個方法獲取其他遊戲部件,或者是其他的腳本。

當獲取其他組件元素時候,建議利用方法獲取組件,然後target.getCompent<BoxCollider>( )

還有一種獲取遊戲部件方式

利用tag標簽:GameObject.FindGameObjectsWithTag(“targetName”)

12.Time.deltaTime( )

Time.deltaTime( )返回值為調用Update( )方法的間隔時間,即為幀間隔時間,所以在特定情況下,

乘以Time.deltaTime( )可以實現按照每一幀更改的目的,達到畫面的連貫性。

13.Instantiate( )

Instantiate(gameObject,target.transform.position,target.transform.rotation)

用於實例化對象在指定位置角度

Rigidbody rb = Instantiate(gameObject,target.transform.position,target.transform.rotation) as Rigidbody;

這樣可以對實例化後的對象進行操作,比如增加受力。rb.AddForce(Vector3)

14.Invoke(“函數名稱”,time)

用於等待time後進行函數調用

15.InvokeReapting( )

InvokeReapting(“函數名稱”,time,durTime)

Time為調用延遲時間

durTime為重復調用間隔時間

想要結束重復,調用方法CancelInvote(“函數名稱”)

16.yield return new WaitForSenconds(3f)

使用這個等待方法的函數需要加上IEnumerator修飾符

在調用時利用StartCoroutine(函數名稱)啟動

17.delegate void Function();

Function() function;

用於方法的疊加調用。
---------------------
作者:唐三十胖子
來源:CSDN
原文:https://blog.csdn.net/iceSony/article/details/77890260
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

【U3D入門小白教程——代碼篇】之三:常見腳本函數