国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

Unity3D獲取當前鍵盤按鍵及Unity3D鼠標、鍵盤的基本操作

2020-01-24 01:23:37
字體:
來源:轉載
供稿:網友

獲取當前鍵盤按鍵,代碼如下:

using UnityEngine;using System.Collections;public class GetCurrentKey : MonoBehaviour { KeyCode currentKey; void Start () {  currentKey = KeyCode.Space; } void OnGUI() {  if (Input.anyKeyDown)  {   Event e = Event.current;   if (e.isKey)   {    currentKey = e.keyCode;    Debug.Log("Current Key is : " + currentKey.ToString());   }  } }}

下面給大家介紹Unity3D鼠標、鍵盤的基本操作

鍵盤:  

GetKey             當通過名稱指定的按鍵被用戶按住時返回true
GetKeyDown   當用戶按下指定名稱的按鍵時的那一幀返回true。
GetKeyUp        在用戶釋放給定名字的按鍵的那一幀返回true。 
GetAxis(“Horizontal")和GetAxis(“Verical”) 用方向鍵或WASD鍵來模擬-1到1的平滑輸入   

鍵盤判斷:   

If(Input.GetKeyDown(KeyCode.A)){//KeyCode表示包含鍵盤所有鍵     
print(“按下A鍵”); }  If(Input.GetKeyUp(KeyCode.D)){//當按D鍵松開時    
print(“松開D鍵”); }  If(Input.GetAxis(“Horizontal")){//當按下水平鍵時   
print(“按下水平鍵”); }  If(Input.GetKeyUp("Verical“)){當按下垂直鍵時    
print(“按下垂直鍵”); }  

鼠標:  

GetButton           根據按鈕名稱返回true當對應的虛擬按鈕被按住時。
GetButtonDown      在給定名稱的虛擬按鈕被按下的那一幀返回true。
GetButtonUp        在用戶釋放指定名稱的虛擬按鈕時返回true。  

鼠標判斷:   

if(Input.GetButton("Fire1")){//Fire1表示按下鼠標左鍵       
print(“按下鼠標左鍵”); }  if (Input.GetMouseButton(0)) {//0表示鼠標左鍵     
Debug.Log("按下鼠標左鍵"); }   if (Input.GetMouseButton(1)) {//1表示鼠標右鍵    
Debug.Log("按下鼠標右鍵");  }  if (Input.GetMouseButton(2)) {//2表示鼠標中鍵    
Debug.Log("按下鼠標中鍵"); } 

給物體施加普通力:   

1、先給物體添加剛體 
2、transform.rigidbody.AddForce(0,0,1000);  一個簡單例子讓小球撞破墻: 

代碼如下: 

using UnityEngine; using System.Collections; public class Cube : MonoBehaviour { // Use this for initialization void Start () { }  // Update is called once per frame void Update () { if(Input.GetKey(KeyCode.W)){//當鼠標按下W鍵時,小球向前移動 transform.Translate(Vector3.forward); } if(Input.GetKey(KeyCode.S)){當鼠標按下S鍵時,小球向后移動  transform.Translate(Vector3.back); 天貓雙十一活動 } if(Input.GetKey(KeyCode.A)){當鼠標按下A鍵時,小球向左移動  transform.Translate(Vector3.left);  }  if(Input.GetKey(KeyCode.D)){當鼠標按下D鍵時,小球向右移動  transform.Translate(Vector3.right);  } if(Input.GetButton("Fire1")){//當點擊鼠標左鍵時,小球撞塌墻  transform.rigidbody.AddForce(0,0,200);//物體向前移動的力為200  }  }  } 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 犍为县| 榆树市| 论坛| 海晏县| 湖口县| 北宁市| 应用必备| 郸城县| 青阳县| 平南县| 塘沽区| 北安市| 西峡县| 榕江县| 南投县| 甘谷县| 丹棱县| 清水河县| 健康| 邵东县| 依安县| 堆龙德庆县| 保德县| 岑溪市| 大安市| 五大连池市| 右玉县| SHOW| 临泽县| 色达县| 普格县| 神农架林区| 辽阳市| 怀安县| 安徽省| 格尔木市| 柘城县| 遂宁市| 乐都县| 乐都县| 黎平县|