第一課程:1.Unity類名必須與文件名保持一致2.講屬性設置為public可以在Unity中訪問 public float speed;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {//獲取左右方向鍵的的值(范圍為-1到1)float amtToMove = Input.GetAxis ("Horizontal") * speed;//使用矩陣進行平移gameObject.transform.Translate (Vector3.right * amtToMove);}
3.攝像機:游戲的輸出畫面是由攝像機所觀測的場景而實現的,將游戲場景呈現到2D的計算機屏幕,有兩種投影方式為透視投影和正交投影,Unity默認為透視投影,透視投影感覺有距離感,正交投影沒有距離感。開發Unity2D游戲,需要將投影方式改為正交投影。
透視投影的三個主要參數:FieldofView(視角),NearClipPlane(近看平面),FarClipPlane(遠看平面)4.GameObject對象包含transform,camera屬性,GetComponet和AddComponent等方法5.Transform實現對象的位置、旋轉以及縮放 position rotation localScale Translate方法 Rotate方法6.Input.GetAxis()與Input.GetAxisRaw()檢測方向鍵 檢測上下移動Input.GetAxis("Vertical") 檢測左右移動Input.GetAxis("Horizontal")7.Time類 deltaTime 上一幀到本幀的時間,單位為秒8.三個Update的調用順序MonoBehaviour.FixedUpdate()MonoBehaviour.Update()MonoBehaviour.LateUpdate()
9.循環移動方塊
public class Player : MonoBehaviour {
public float playerSpeed;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {Debug.Log ("Update");var moveto = Input.GetAxis ("Horizontal") *Time.deltaTime* playerSpeed;gameObject.transform.Translate (Vector3.right * moveto);
if (transform.position.x > 9.15) {transform.position=new Vector3(-9.15f,transform.position.y);}
if (transform.position.x <- 9.15) {transform.position=new Vector3(9.15f,transform.position.y);}}
void LateUpdate(){Debug.Log ("LateUpdate");}
void FixedUpdate(){Debug.Log("FixedUpdate");}}9.創建按鈕并響應按鈕操作
void OnGUI(){if (GUI.Button (new Rect (0, 0, 100, 50), "Play")) {
} else if (GUI.Button (new Rect (0, 60, 100, 50), "Pause")) {} else if (GUI.Button (new Rect (0, 120, 100, 50), "Stop")) {}}
新聞熱點
疑難解答