当前位置:   article > 正文

【Unity3D】人机交互Input_unity input

unity input

1 前言

        Input 是 Unity3D 中用于人机交互的工具类,用户可以调用其 GetKey、GetMousePosition、GetMouseButton、GetAxis、GetButton 等方法获取键盘和鼠标的状态信息,再通过这些状态信息控制游戏对象,从而实现人机交互。

        1)键盘输入

  1. // 按住按键
  2. public static bool GetKey(KeyCode key)
  3. // 按下按键
  4. public static bool GetKeyDown(KeyCode key)
  5. // 抬起按键
  6. public static bool GetKeyUp(KeyCode key)

        KeyCode 的取值有:

  1. A~Z
  2. F1~F15
  3. // 键盘顶部的数字
  4. Alpha0~Alpha9
  5. Left、RightArrow、UpArrow、DownArrow
  6. LeftCtrl、LeftShift、LeftAlt、LeftWindows、RightCtrl、RightShift、RightAlt、RightWindows
  7. Tab、Space、Backspace、Return
  8. // 加号、减号、星号、斜杠、反斜杠、左括号、右括号、小于号、大于号、等于号、上尖号
  9. Plus、Minus、Asterisk、Slash、Backslash、LeftBracket、RightBracket、Less、Greater、Equals、Caret
  10. // 逗号、点号、问号、分号、冒号、单引号、反引号、双引号、感叹号
  11. Comma、Period、Question、Semicolon、Colon、Quote、BackQuote、DoubleQuote、Exclaim
  12. // @符号、$符号、&符号、下划线
  13. At、Dollar、Ampersand、Underscore
  14. Insert、Delete、Home、End、PageUp、PageDown、Print
  15. CapsLock、Numlock、ScrollLock
  16. Keypad0~Keypad9、KeypadPeriod
  17. KeypadPlus、KeypadMinus、KeypadMultiply、KeypadDivide
  18. KeypadEquals、KeypadEnter
  19. // 鼠标左键、鼠标右键、鼠标中间
  20. Mouse0、Mouse1、Mouse2

        2)鼠标输入

  1. // 按住鼠标
  2. public static bool GetMouseButton(int button)
  3. // 按下鼠标
  4. public static bool GetMouseButtonDown(int button)
  5. // 抬起鼠标
  6. public static bool GetMouseButtonUp(int button)
  7. // 鼠标坐标
  8. Vector3 position = Input.mousePosition

        说明:button 取值为 0、1、2,分别表示鼠标左键、右键、中键。 

        3)虚拟轴输入

        在【Edit→Project Settings→Input】中可以打开 InputManager 配置界面,用户可以在此配置虚拟轴信息。

      

  1. // 按左右箭头或A、D键,hor在-1~1之间变化
  2. float hor = Input.GetAxis("Horizontal");
  3. // 按上下箭头或W、S键,hor在-1~1之间变化
  4. float ver = Input.GetAxis("Vertical");
  5. // 获取鼠标在水平方向上的移动
  6. float mouseX = Input.GetAxis("Mouse X");
  7. // 获取鼠标在竖直方向上的移动
  8. float mouseY = Input.GetAxis("Mouse Y");
  9. // 获取滚轮信息, 上滑为正, 下滑为负
  10. float scroll = Input.GetAxis("Mouse ScrollWheel");

        4)虚拟按键输入

         InputManager 配置界面配置虚拟按键,如下:

  1. // 按住虚拟按键
  2. public static bool GetButton(string buttonName)
  3. // 按下虚拟按键
  4. public static bool GetButtonDown(string buttonName)
  5. // 抬起虚拟按键
  6. public static bool GetButtonUp(string buttonName)
  7. // 按以上配置,按住Q键或鼠标左键返回true,表示开火了
  8. bool fire = Input.GetButton("Fire");

        5)鼠标回调方法

  1. using UnityEngine;
  2. public class MouseEventTest : MonoBehaviour {
  3. private void OnMouseEnter() { // 鼠标进入碰撞体时回调
  4. Debug.Log("OnMouseEnter");
  5. }
  6. private void OnMouseOver() { // 鼠标悬浮在碰撞体上回调
  7. Debug.Log("OnMouseOver");
  8. }
  9. private void OnMouseExit() { // 鼠标离开碰撞体时回调
  10. Debug.Log("OnMouseExit");
  11. }
  12. private void OnMouseDown() { // 鼠标在碰撞体上按下了左键时回调
  13. Debug.Log("OnMouseDown");
  14. }
  15. private void OnMouseUp() { // 鼠标在碰撞体上按下了左键, 并且抬起时回调(此时不一定悬浮在碰撞体上)
  16. Debug.Log("OnMouseUp");
  17. }
  18. private void OnMouseUpAsButton() { // 鼠标按下和抬起都在同一个碰撞体上, 并且抬起了鼠标左键时回调
  19. Debug.Log("OnMouseUpAsButton");
  20. }
  21. private void OnMouseDrag() { // 鼠标在碰撞体上按下, 并拖拽时回调(拖拽过程中不一定悬浮在碰撞体上)
  22. Debug.Log("OnMouseDrag");
  23. }
  24. }

        说明:MouseEventTest 挂在需要触发鼠标事件的对象上,并且该对象有碰撞体组件,2D UI 需要添加 BoxCollider2D 组件。

2 应用

        本节将实现坦克对战游戏。

        1)实现需求

  • 绘制2个坦克,一个表示己方,一个表示敌方;
  • 用户可以通过上下箭头按键控制己方坦克前后移动,通过左右箭头按键控制己方坦克左右移动或左右转向;
  • 用户可以通过鼠标左键发射炮弹;
  • 敌方坦克可以自动转向瞄准己方坦克,但是有延时,转向先快后慢,延时插值系数为 0.6;
  • 敌方坦克瞄准己方坦克后(允许有5°误差),自动开炮,开炮间隔需要大于1秒。

        2)创建游戏对象

        游戏对象的 Transform 组件参数如下: 

nametypepositionRotationscaleColor/Texture
MyTankEmpty(0, 0.25, -5)(0, 0, 0)(1, 1, 1)——
ButtonCube(0, 0, 0)(0, 0, 0)(2, 0.5, 2)#228439FF
TopCube(0, 0.5, 0)(0, 0, 0)(1, 0.5, 1)#228439FF
GunCylinder(0, 0, 1.5)(90, 0, 0)(0.2, 1, 0.4)#228439FF
FirePointEmpty(0, 1.15, 0)(0, 0, 0)(1, 1, 1)——
MyBulletSphere(0, 0, -2)(0, 0, 0)(0.2, 0.2, 0.2)#82EA4FFF
PlanePlane(0, 0, 0)(0, 0, 0)(10, 10, 10)GrassRockyAlbedo
EnemyTankEmpty(0, 0.25, 5)(0, 180, 0)(1, 1, 1)15D3F9FF
EnemyBulletSphere(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

  1. using UnityEngine;
  2. public class MyTank : MonoBehaviour {
  3. private Transform firePoint; // 开火点
  4. private GameObject bulletPrefab; // 炮弹预设体
  5. void Start() {
  6. firePoint = transform.Find("Top/Gun/FirePoint");
  7. bulletPrefab = (GameObject) Resources.Load("Prefabs/MyBullet");
  8. Debug.Log(bulletPrefab);
  9. }
  10. void Update () {
  11. float hor = Input.GetAxis("Horizontal");
  12. float ver = Input.GetAxis("Vertical");
  13. move(hor, ver);
  14. if (Input.GetMouseButtonDown(0)) { // 开炮
  15. GameObject bullet = Instantiate(bulletPrefab, firePoint.position, Quaternion.identity); // 通过预设体创建炮弹
  16. bullet.GetComponent<Bullet>().setMoveDir(transform.forward); // 设置炮弹飞出方向
  17. }
  18. }
  19. private void move(float hor, float ver) {
  20. // 移动方案一:上下箭头控制前后移动,左右箭头控制左右移动
  21. // transform.Translate(hor * Time.deltaTime * 3, 0, ver * Time.deltaTime * 3);
  22. // 移动方案二:上下箭头控制前后移动,左右箭头控制左右拐弯
  23. transform.Translate(0, 0, ver * Time.deltaTime * 3);
  24. transform.Rotate(Vector3.up * hor * Time.deltaTime * 120f);
  25. }
  26. }

         说明:MyTank.cs 脚本组件挂载在 MyTank 游戏对象上。

        EnemyTank.cs

  1. using UnityEngine;
  2. public class EnemyTank : MonoBehaviour {
  3. private Transform target; // 目标
  4. private Transform top; // 炮头
  5. private Transform firePoint; // 开火点
  6. private GameObject bulletPrefab; // 炮弹预设体
  7. private float fireInternal = 0; // 开炮间隔
  8. void Start () {
  9. target = GameObject.Find("MyTank/Top").transform;
  10. top = transform.Find("Top");
  11. firePoint = transform.Find("Top/Gun/FirePoint");
  12. bulletPrefab = (GameObject) Resources.Load("Prefabs/EnemyBullet");
  13. }
  14. void Update () {
  15. Quaternion dir = Quaternion.LookRotation(target.position - top.position);
  16. top.rotation = Quaternion.Lerp(top.rotation, dir, Time.deltaTime * 0.6f); // 敌军转向己方
  17. float angle = Vector3.Angle(target.position - top.position, top.forward);
  18. if (angle < 5 && fireInternal > 1) {
  19. GameObject bullet = Instantiate(bulletPrefab, firePoint.position, Quaternion.identity); // 通过预设体创建炮弹
  20. bullet.GetComponent<Bullet>().setMoveDir(top.forward); // 设置炮弹飞出方向
  21. fireInternal = 0;
  22. }
  23. fireInternal += Time.deltaTime;
  24. }
  25. }

         说明:EnemyTank.cs 脚本组件挂载在 EnemyTank 游戏对象上。

        Bullet.cs

  1. using UnityEngine;
  2. public class Bullet : MonoBehaviour {
  3. private Vector3 moveDir; // 炮弹飞出方向
  4. void Start () {
  5. Destroy(gameObject, 2); // 2秒后自动销毁
  6. }
  7. void Update () {
  8. if (moveDir != null) {
  9. transform.Translate(moveDir * Time.deltaTime * 6);
  10. }
  11. }
  12. public void setMoveDir(Vector3 dir) {
  13. moveDir = dir;
  14. }
  15. }

         说明:Bullet.cs 脚本组件挂载在 MyBullet 和 EnemyBullet 预设体上。

          4)运行效果

        左右箭头按键控制己方坦克左右移动 

        左右箭头按键控制己方坦克左右转向

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/91776
推荐阅读
相关标签
  

闽ICP备14008679号