iLMS知識社群歷程檔登入
位置: 黃國哲 > Unity
1121219 角色移動
by 黃國哲 2023-12-19 09:06:06, 回應(0), 人氣(163)
https://jayjustice1945.blogspot.com/2016/09/unityc.html

private void movement()//方法
	  {   //採用直接改變物件座標的方式
				//一、向右走
     if (Input.GetKey("d"))//輸入.來自鍵盤(“d”)
     {
         this.gameObject.transform.Translate(new Vector2(5, 0) );
     }  //此類別.這個物件.座標系統.位移(delta向量)

				//二、向左走;依照一、的作法會發現物件飆很快,因此要乘上Time.deltaTime來延遲。
		 if (Input.GetKey("a"))
     {
        this.gameObject.transform.Translate(new Vector2(-5, 0) * Time.deltaTime);
     }  
				//向上走 //可以直接使用Vector的屬性Vector2.up,就不需要new一個變數
     if (Input.GetKey("w"))
     {
        this.gameObject.transform.Translate(Vector2.up * Time.deltaTime);
     }  
				//向下走
		 if (Input.GetKey("s"))
     {
        this.gameObject.transform.Translate(Vector2.down * Time.deltaTime);
     }  

  }
回應