【Unity3D】人机交互Input
1 前言
????????Input 是 Unity3D 中用于人機交互的工具類,用戶可以調用其 GetKey、GetMousePosition、GetMouseButton、GetAxis、GetButton 等方法獲取鍵盤和鼠標的狀態信息,再通過這些狀態信息控制游戲對象,從而實現人機交互。
????????1)鍵盤輸入
// 按住按鍵 public static bool GetKey(KeyCode key) // 按下按鍵 public static bool GetKeyDown(KeyCode key) // 抬起按鍵 public static bool GetKeyUp(KeyCode key)? ? ? ? KeyCode 的取值有:
A~Z F1~F15 // 鍵盤頂部的數字 Alpha0~Alpha9 Left、RightArrow、UpArrow、DownArrow LeftCtrl、LeftShift、LeftAlt、LeftWindows、RightCtrl、RightShift、RightAlt、RightWindows Tab、Space、Backspace、Return // 加號、減號、星號、斜杠、反斜杠、左括號、右括號、小于號、大于號、等于號、上尖號 Plus、Minus、Asterisk、Slash、Backslash、LeftBracket、RightBracket、Less、Greater、Equals、Caret // 逗號、點號、問號、分號、冒號、單引號、反引號、雙引號、感嘆號 Comma、Period、Question、Semicolon、Colon、Quote、BackQuote、DoubleQuote、Exclaim // @符號、$符號、&符號、下劃線 At、Dollar、Ampersand、Underscore Insert、Delete、Home、End、PageUp、PageDown、Print CapsLock、Numlock、ScrollLock Keypad0~Keypad9、KeypadPeriod KeypadPlus、KeypadMinus、KeypadMultiply、KeypadDivide KeypadEquals、KeypadEnter // 鼠標左鍵、鼠標右鍵、鼠標中間 Mouse0、Mouse1、Mouse2????????2)鼠標輸入
// 按住鼠標 public static bool GetMouseButton(int button) // 按下鼠標 public static bool GetMouseButtonDown(int button) // 抬起鼠標 public static bool GetMouseButtonUp(int button) // 鼠標坐標 Vector3 position = Input.mousePosition? ? ? ? 說明:button 取值為 0、1、2,分別表示鼠標左鍵、右鍵、中鍵。?
????????3)虛擬軸輸入
? ? ? ? 在【Edit→Project Settings→Input】中可以打開 InputManager 配置界面,用戶可以在此配置虛擬軸信息。
??????
// 按左右箭頭或A、D鍵,hor在-1~1之間變化 float hor = Input.GetAxis("Horizontal"); // 按上下箭頭或W、S鍵,hor在-1~1之間變化 float ver = Input.GetAxis("Vertical"); // 獲取鼠標在水平方向上的移動 float mouseX = Input.GetAxis("Mouse X"); // 獲取鼠標在豎直方向上的移動 float mouseY = Input.GetAxis("Mouse Y"); // 獲取滾輪信息, 上滑為正, 下滑為負 float scroll = Input.GetAxis("Mouse ScrollWheel");????????4)虛擬按鍵輸入
?????????InputManager 配置界面配置虛擬按鍵,如下:
// 按住虛擬按鍵 public static bool GetButton(string buttonName) // 按下虛擬按鍵 public static bool GetButtonDown(string buttonName) // 抬起虛擬按鍵 public static bool GetButtonUp(string buttonName) // 按以上配置,按住Q鍵或鼠標左鍵返回true,表示開火了 bool fire = Input.GetButton("Fire");2 應用
? ? ? ? 本節將實現坦克對戰游戲。
? ? ? ? 1)實現需求
- 繪制2個坦克,一個表示己方,一個表示敵方;
- 用戶可以通過上下箭頭按鍵控制己方坦克前后移動,通過左右箭頭按鍵控制己方坦克左右移動或左右轉向;
- 用戶可以通過鼠標左鍵發射炮彈;
- 敵方坦克可以自動轉向瞄準己方坦克,但是有延時,轉向先快后慢,延時插值系數為 0.6;
- 敵方坦克瞄準己方坦克后(允許有5°誤差),自動開炮,開炮間隔需要大于1秒。
? ? ? ? 2)創建游戲對象
? ? ? ? 游戲對象的 Transform 組件參數如下:?
| name | type | position | Rotation | scale | Color/Texture |
| MyTank | Empty | (0, 0.25, -5) | (0, 0, 0) | (1, 1, 1) | —— |
| Button | Cube | (0, 0, 0) | (0, 0, 0) | (2, 0.5, 2) | #228439FF |
| Top | Cube | (0, 0.5, 0) | (0, 0, 0) | (1, 0.5, 1) | #228439FF |
| Gun | Cylinder | (0, 0, 1.5) | (90, 0, 0) | (0.2, 1, 0.4) | #228439FF |
| FirePoint | Empty | (0, 1.15, 0) | (0, 0, 0) | (1, 1, 1) | —— |
| MyBullet | Sphere | (0, 0, -2) | (0, 0, 0) | (0.2, 0.2, 0.2) | #82EA4FFF |
| Plane | Plane | (0, 0, 0) | (0, 0, 0) | (10, 10, 10) | GrassRockyAlbedo |
| EnemyTank | Empty | (0, 0.25, 5) | (0, 180, 0) | (1, 1, 1) | 15D3F9FF |
| EnemyBullet | Sphere | (0, 0, 2) | (0, 0, 0) | (0.2, 0.2, 0.2) | #4C55F8FF |
? ? ? ? 說明:?EnemyTank 由?MyTank復制而來,只是修改了它自己及其子對象的顏色屬性;EnemyBullet 由?MyBullet 復制而來,然后修改了顏色屬性;將?MyBullet 和 EnemyBullet 拖拽至 Assets 窗口的 Resources/Prefabs 目錄下,生成預設體(prefab),再刪除 Hierarchy 窗口下?MyBullet 和 EnemyBullet 對象。
? ? ? ? ?游戲對象的層級結構如下:
? ? ? ? 游戲界面如下:
? ? ? ? ?3)腳本組件
????????MyTank.cs
using UnityEngine;public class MyTank : MonoBehaviour {private Transform firePoint; // 開火點private GameObject bulletPrefab; // 炮彈預設體void Start() {firePoint = transform.Find("Top/Gun/FirePoint");bulletPrefab = (GameObject) Resources.Load("Prefabs/MyBullet");Debug.Log(bulletPrefab);}void Update () {float hor = Input.GetAxis("Horizontal");float ver = Input.GetAxis("Vertical");move(hor, ver);if (Input.GetMouseButtonDown(0)) { // 開炮GameObject bullet = Instantiate(bulletPrefab, firePoint.position, Quaternion.identity); // 通過預設體創建炮彈bullet.GetComponent<Bullet>().setMoveDir(transform.forward); // 設置炮彈飛出方向}}private void move(float hor, float ver) {// 移動方案一:上下箭頭控制前后移動,左右箭頭控制左右移動// transform.Translate(hor * Time.deltaTime * 3, 0, ver * Time.deltaTime * 3);// 移動方案二:上下箭頭控制前后移動,左右箭頭控制左右拐彎transform.Translate(0, 0, ver * Time.deltaTime * 3);transform.Rotate(Vector3.up * hor * Time.deltaTime * 120f);} }? ? ? ? ?說明:MyTank.cs 腳本組件掛載在 MyTank 游戲對象上。
????????EnemyTank.cs
using UnityEngine;public class EnemyTank : MonoBehaviour {private Transform target; // 目標private Transform top; // 炮頭private Transform firePoint; // 開火點private GameObject bulletPrefab; // 炮彈預設體private float fireInternal = 0; // 開炮間隔void Start () {target = GameObject.Find("MyTank/Top").transform;top = transform.Find("Top");firePoint = transform.Find("Top/Gun/FirePoint");bulletPrefab = (GameObject) Resources.Load("Prefabs/EnemyBullet");}void Update () {Quaternion dir = Quaternion.LookRotation(target.position - top.position);top.rotation = Quaternion.Lerp(top.rotation, dir, Time.deltaTime * 0.6f); // 敵軍轉向己方float angle = Vector3.Angle(target.position - top.position, top.forward);if (angle < 5 && fireInternal > 1) {GameObject bullet = Instantiate(bulletPrefab, firePoint.position, Quaternion.identity); // 通過預設體創建炮彈bullet.GetComponent<Bullet>().setMoveDir(top.forward); // 設置炮彈飛出方向fireInternal = 0;}fireInternal += Time.deltaTime;} }? ? ? ? ?說明:EnemyTank.cs 腳本組件掛載在 EnemyTank 游戲對象上。
????????Bullet.cs
using UnityEngine;public class Bullet : MonoBehaviour {private Vector3 moveDir; // 炮彈飛出方向void Start () {Destroy(gameObject, 2); // 2秒后自動銷毀}void Update () {if (moveDir != null) {transform.Translate(moveDir * Time.deltaTime * 6);}}public void setMoveDir(Vector3 dir) {moveDir = dir;} }? ? ? ? ?說明:Bullet.cs 腳本組件掛載在 MyBullet 和 EnemyBullet 預設體上。
?? ? ? ? ?4)運行效果
????????左右箭頭按鍵控制己方坦克左右移動?
????????左右箭頭按鍵控制己方坦克左右轉向
總結
以上是生活随笔為你收集整理的【Unity3D】人机交互Input的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win2003服务器端口修改,Windo
- 下一篇: 浏览器数据库 IndexedDB