我的學習第三章節:常見腳本
ide
1.Awake( )與Start()函數
Awake( )在遊戲建立時調用,用於設置遊戲初始化的參數性能
Start( )在腳本被調用,執行在全部Update( )以前,用於賦值變量學習
2.Update( )與FixedUpdate().net
Update( )用於非剛體對象的運動,諸如定時器,檢測輸入等,Update受制於機器性能幀率orm
FixedUpdate()用於剛體對象運動,不受制於機器性能,有固定調用間隔。對象
3.Vector3.Dot( )與Vector3.Cross( )blog
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中
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;