物體移動
- translate(X,Y,Z)移動
void Update () {
float x=Input.GetAxis("Horizontal") * Time.deltaTime * speed;
float z= Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(x, 0,z);
}
- 剛體加力移動
//按下鼠標左鍵
if (Input.GetButtonDown("Fire1"))
{
//新建預制物體,預制對象,位置,旋轉速度
Transform tf=Instantiate(newtf, transform.position, transform.rotation);
//剛體加力
tf.GetComponent<Rigidbody>().AddForce(transform.TransformDirection(Vector3.forward)*force);
}
- 設置剛體速度
gameObject.GetComponent<Rigidbody>().velocity=new Vector3(0,y,0);
物體旋轉
//檢測鍵盤輸入
if (Input.GetKey(KeyCode.Q))
{
//物體旋轉
//左右旋轉,即以y軸旋轉;
//Space.Self以自身坐標軸旋轉,Space.World以世界坐標軸旋轉
transform.Rotate(0, -1*rotateSpeed * Time.deltaTime, 0, Space.Self);
}
if(Input.GetKey(KeyCode.E))
{
transform.Rotate(0, rotateSpeed*Time.deltaTime, 0, Space.Self);
}
查找物體
//fps為name,text為類型
//fps顯示方式是欠妥的
GameObject.Find("fps").GetComponent<Text>().text =
"FPS " + (1 / Time.deltaTime).ToString().Substring(0,2);
//尋找腳本組件
GameObject.FindGameObjectWithTag("MainCamera")
.GetComponent<ClassName>().enabled = false;
繪制按鈕
private void OnGUI(){
if (GUI.Button(new Rect((float)(Screen.width*0.3),
(float)(Screen.width*0.1),
(float)(Screen.width*0.4),
(float)(Screen.width*0.05)),
"退出")){
//退出程序
Application.Quit();
}
if (GUI.Button(new Rect((float)(Screen.width*0.3),
(float)(Screen.width*0.2),
(float)(Screen.width*0.4),
(float)(Screen.width*0.05)),
"重新開始")){
//加載場景
SceneManager.LoadScene("1");
}
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。