Unity解决:3D开发模式第三人称视角 WASD控制角色移动旋转 使用InputSystem

本文主要是介绍Unity解决:3D开发模式第三人称视角 WASD控制角色移动旋转 使用InputSystem,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Unity版本:2019.2.3f1

目录

 安装InputSystem

1:创建InputHander.cs脚本 挂载到Player物体上 获取键盘输入WADS

2.创建PlayerLocomotion.cs挂载到Player物体上,控制物体移动转向


 

 安装InputSystem

菜单栏/Window/Package Manager/Input System

 工程面板内 右键-->创建Input Actions 

选中New Controls改名为PlayerControls 然后属性 面板按下Edit asset

 Action Maps添加:PlayerMovement

Actions添加:New action 改名为MovementAction 

Properties项    修改ActionType=Pass Through

                        修改ControlType= Vector2

在MovementAction项点击+号 选择Add 2D Vector Composite

 

 生成WASD

绑定Up、Down、Left、Right,如此类推

回到PlayerControls属性面板 勾选Generate C# Class[*]

 

 工程面板就生成了一份 PlayerControls.cs 脚本,先不管它 

 

 

然后创建2个脚本挂到Player物体上

1:创建InputHander.cs脚本 挂载到Player物体上 获取键盘输入WADS

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class InputHander : MonoBehaviour
{public float horizontal;public float vertical;public float moveAmount;public float mouseX;public float mouseY;PlayerControls inputActions;//声明 InputSystem的脚本对象Vector2 movementInput;//存储 WASD输入的值public void OnEnable(){//获取设备上的输入if (inputActions == null){inputActions = new PlayerControls();//绑定输入的值inputActions.PlayerMovement.MovementAction.performed += outputActions => movementInput = outputActions.ReadValue<Vector2>();}inputActions.Enable();//启用}public void OnDisable(){inputActions.Disable();//禁用}public void TickInputt(float delta){MoveInput(delta);}public void MoveInput(float delta) {horizontal = movementInput.x;vertical = movementInput.y;moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));}}

2.创建PlayerLocomotion.cs挂载到Player物体上,控制物体移动转向

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerLocomotion : MonoBehaviour
{InputHander inputHander;Vector3 moveDirection;public CapsuleCollider capsuleCollider;public new Rigidbody rigidbody;Vector2 movementInput;//存储 WASD输入的值Transform cameraObject;[HideInInspector]public Transform myTransform;[Header("Stats")][SerializeField]float movementSpeed = 5;[SerializeField]float rotationSpeed = 10;// Start is called before the first frame updatevoid Start(){rigidbody = GetComponent<Rigidbody>();if (rigidbody==null){rigidbody = gameObject.AddComponent<Rigidbody>();rigidbody.constraints = RigidbodyConstraints.FreezeRotation;}capsuleCollider = GetComponent<CapsuleCollider>();if (capsuleCollider==null){capsuleCollider = gameObject.AddComponent<CapsuleCollider>();capsuleCollider.center = new Vector3(0, 1, 0);capsuleCollider.radius = 0.5f;capsuleCollider.height = 2;capsuleCollider.direction = 1;}inputHander = GetComponent<InputHander>();cameraObject = Camera.main.transform;myTransform = transform;}// Update is called once per framevoid Update(){float delta = Time.deltaTime;inputHander.TickInputt(delta);moveDirection = cameraObject.forward * inputHander.vertical;moveDirection += cameraObject.right * inputHander.horizontal;moveDirection.Normalize();float speed = movementSpeed;moveDirection *= speed;//移动Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, new Vector3(0, 0, 0));rigidbody.velocity = projectedVelocity;HandleRotation(delta);}//转向private void HandleRotation(float delta) {Vector3 targerDir = Vector3.zero;targerDir = cameraObject.forward * inputHander.vertical;targerDir += cameraObject.right * inputHander.horizontal;targerDir.Normalize();targerDir.y = 0;if (targerDir == Vector3.zero){targerDir = myTransform.forward;}Quaternion tr = Quaternion.LookRotation(targerDir);Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rotationSpeed * delta);myTransform.rotation = targetRotation;}
}

完成

这篇关于Unity解决:3D开发模式第三人称视角 WASD控制角色移动旋转 使用InputSystem的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/228280

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一