1.陣列命名public GameObject[] XXXX;
2.滑鼠輸入if (Input.GetMouseButtonDown(0)){}//0是左鍵1是右鍵
3.鍵盤輸入if (Input.GetKeyDown("space")){}//左”left”右”rigrt”下”down”上”up”
4.陣列長度 XXXXX.Length
5.控制攝影機:
public GameObject[] gameCameras; private int gameCameraIndex = 0; void Start () { FocusOnCamera(gameCameraIndex); } void Update () { if (Input.GetMouseButtonDown(0)) { CangeCamera(1); } if (Input.GetMouseButtonDown(1)){ CangeCamera(-1); }
6. 遊戲物件改名子
public float ScalSize = 2.5f;
public string NewName ="hi cube";
// Use this for initialization
void Start () {
transform.name = Imput(NewName);
}
// Update is called once per frame
void Update () {
transform.localScale =Vector3.one* ScalSize;
}
string Imput(string name)
{
return"-{"+name+"}-";
}
}
7. 鋼體增加一個力:
GetComponent<Rigidbody>().AddForce(0,0,0);
8.碰撞偵測:
void OnCollisionEnter(Collision collision)
{ if (collision.transform.name == "XXXX")}
9.亂數:
Random.Range(-10f, 10f);
10. While:
while (a < 10) { GameObject WhileCube = Instantiate(player_1); WhileCube.transform.position = new Vector3( Random.Range(-5f, 5f), Random.Range(-5f, 5f), 0);//生成新位子 a++; }
11. 攝影機追隨跟隨物件:
public Transform target; void Update () { transform.LookAt(target); }
12. Switch case用法
switch(level) { case 10: case 9: System.out.println("得A"); break; case 8: System.out.println("得B"); break; case 7: System.out.println("得C"); break; case 6: System.out.println("得D"); break; default: System.out.println("得E(不及格)"
13. 時間倒數: XXX -= Time.deltaTime;
14把原本的float的infotext轉成int:infotext.text = (int).esetTimer + "秒後重新開始";
15. RayCast射線:
Physics.Raycast (origin : Vector3, direction : Vector3, hitInfo :RaycastHit ,distance :float ,LayerMesk:int)
Physics.Raycast(射線初始位置,射線方向,儲存所碰到物件,射線長度(沒設置。無限長),設定忽略物件)
16. private Vector3 XXX;
//預設值0,0,0
17.|| or的意思
18.使用 3D的Text :
public TextMesh XX;改變XX.text=”TTT”;
19.抓取子物件內容有Enemy程式有幾個:
int enemiesRemaining = enemyContainer.GetComponentsInChildren<Enemy>().Length;
20.用hit去抓有DoorButton的程式
(hit.transform.GetComponent<DoorButton>() != null
21. 1. 四捨五入(0.4以下捨去5以上進位)
Mathf.Round(2.2f) 輸出: 2
Mathf.Round(2.7f) 輸出: 3
22. 無條件進位(直接進位)
Mathf.Ceil(3.5f) 輸出: 4
Mathf.Ceil(6.7f) 輸出: 7
23. 無條件捨去(直接捨去)
Mathf.Floor(7.5f) 輸出: 7
Mathf.Floor(9.3f) 輸出: 9
13