当前位置: 首页 > article >正文

Unity使用新输入系统控制物体移动

介绍

Unity 的 新输入系统(Input System) 是一个强大且灵活的输入管理工具,支持多种输入设备(如键盘、鼠标、手柄、触摸屏等),并且比旧版输入系统(UnityEngine.Input)更现代化和可扩展

使用

1. 安装 Input System 包

打开 Unity 编辑器。

进入 Window > Package Manager。

在 Package Manager 中搜索 Input System。

点击 Install 安装 Input System 包。
在这里插入图片描述

2. 启用 Input System

打开 Edit > Project Settings > Player。

在 Other Settings 下找到 Active Input Handling。

将其设置为 Input System (New) 或 Both(如果需要同时支持旧版和新版输入系统)。

重启 Unity 编辑器以应用更改。
在这里插入图片描述

3. 创建 Input Actions

在物体上添加组件”Player Input“,点击”Create Actions“,保存到”Setting/InputControl“文件夹下,并生成对应脚本
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4. 在代码中使用 Input System

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerControl : MonoBehaviour
{
    private PlayerInputControl inputControl;
    private Vector2 inputDirection;
    private Rigidbody2D rb;
    private float speed = 20;

    private void Awake()
    {
        inputControl = new PlayerInputControl();
        rb = GetComponent<Rigidbody2D>();
    }

    private void OnEnable()
    {
        inputControl.Enable();
    }

    private void OnDisable()
    {
        inputControl.Disable();
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        inputDirection = inputControl.Player.Move.ReadValue<Vector2>();
    }

    private void FixedUpdate()
    {
        rb.velocity = new Vector2(speed * inputDirection.x * Time.deltaTime, rb.velocity.y);
    }
}


http://www.kler.cn/a/541136.html

相关文章:

  • 力扣 单词拆分
  • 尝试一下,交互式的三维计算python库,py3d
  • 循环神经网络学习01——transformer:输入部分-嵌入层位置编码
  • DeepSeek训练成本与技术揭秘
  • Swift的方法派发机制
  • 深度学习|表示学习|Instance Normalization 全面总结|26
  • VUE项目中实现权限控制,菜单权限,按钮权限,接口权限,路由权限,操作权限,数据权限实现
  • 基于yolo的视频检测分析
  • Linux网络编程--Udp套接字+实战 (万字详解,超详细!!)
  • 【多线程-第三天-NSOperation和GCD的区别 Objective-C语言】
  • 游戏启动不了了?一步步解决kaeon.dll丢失故障
  • VSCode + Continue 实现AI编程助理
  • 四、OSG学习笔记-基础图元
  • 【前端基础】深度理解JavaScript中的异步机制
  • React(三)
  • 每日一题——插入排序实现数据流中的中位数
  • 【Java】多线程和高并发编程(四):阻塞队列(上)基础概念、ArrayBlockingQueue
  • React Hooks 与 Class 组件相比有何优势
  • 速度超越DeepSeek!Le Chat 1100tok/s闪电回答,ChatGPT 4o和DeepSeek R1被秒杀?
  • Haskell语言的云计算
  • 优化GPT API接口链接的方法
  • 解决 npm : 无法加载文件 D:\nodeJS\node_global\npm.ps1,因为在此系统上禁止运行脚本。
  • Android Studio:如何利用Application操作全局变量
  • 用 Java 轻松读取 Word 文档内容
  • dolphinscheduler安装部署
  • Kotlin Bytedeco OpenCV 图像图像51.2 光流背景消除