這幾天經過Unity官網的Unity Scripting Tutorials的視頻學習Unity腳本,觀看的過程當中作了記錄。如今,整理了一下筆記,供本身之後和其餘初學者參考。編輯器
使用這兩個方法控制物體運動。
Translate對應position:ide
transform.Translate(Vector3); // Amount in each axis to move by
Rotate對應rotation:學習
transform.Rotate(Vector3, // Axis around which to rotate float) // amount to rotate by
transform.LookAt(target);
用於移動物體Z軸使之對準目標物體。
將其綁定到相機上能夠實現運動物體的跟蹤。3d
用於平滑某種轉變。code
空間移動:orm
Vector3.Lerp(Vector3 from, // 起點。一般應設爲當前座標 Vector3 to, // 終點 float t); // 0~1之間的值。值越大,返回值越接近終點,移動也越快。
數值變換:視頻
Mathf.Lerp(float from, float to, float t);
顏色漸變:對象
Color.Lerp(Color from, Color to, float t);
GetButton和GetKey的區別:ip
GetButton/Up/Down的區別:字符串
GetAxis(axisName)經過獲取某個軸的值來了解用戶的輸入。
鼠標在Collider或GUI組件上按下時調用該方法。
同類方法:
建立prefab的副本,返回對象的引用。
使用時先用代碼建立GameObject的變量,而後回到編輯器界面將prefab拖入變量中。
調用名叫method的方法:
Invoke(method, delay);
每隔time重複調用method方法:
InvokeRepeating(method, delay, time);
取消Invoke:
CancelInvoke();
取消名叫method的Invoke:
CancelInvoke(method);
利用了C#的yield
,來實如今屢次Update中執行一個行爲。
開始一個Coroutine:
StartCoroutine(IEnumerator routine);
或者接受一個string參數表示方法的返回IEnumerator的方法的名字,後面跟着參數。
例:
StartCoroutine(Func(param)); StartCoroutine("Func", param);
中止一個Coroutine,用StopCoroutine。一樣有兩種形式。
用來表示旋轉的類。講了三個內容:
LookRotation(Vector3 forward);
返回使物體轉向forward方向的Quaternion。
Slerp(Quaternion from, Quaternion to, float t);
旋轉角度。相比Lerp,在中間時快,兩頭時慢。
Quaternion.identity
是一個靜態變量,表示沒有旋轉。