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

相关文章

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Springboot3 ResponseEntity 完全使用案例

《Springboot3ResponseEntity完全使用案例》ResponseEntity是SpringBoot中控制HTTP响应的核心工具——它能让你精准定义响应状态码、响应头、响应体,相比... 目录Spring Boot 3 ResponseEntity 完全使用教程前置准备1. 项目基础依赖(M

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco

解决idea启动项目报错java: OutOfMemoryError: insufficient memory

《解决idea启动项目报错java:OutOfMemoryError:insufficientmemory》:本文主要介绍解决idea启动项目报错java:OutOfMemoryError... 目录原因:解决:总结 原因:在Java中遇到OutOfMemoryError: insufficient me

maven异常Invalid bound statement(not found)的问题解决

《maven异常Invalidboundstatement(notfound)的问题解决》本文详细介绍了Maven项目中常见的Invalidboundstatement异常及其解决方案,文中通过... 目录Maven异常:Invalid bound statement (not found) 详解问题描述可

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

C# 预处理指令(# 指令)的具体使用

《C#预处理指令(#指令)的具体使用》本文主要介绍了C#预处理指令(#指令)的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1、预处理指令的本质2、条件编译指令2.1 #define 和 #undef2.2 #if, #el

C#中Trace.Assert的使用小结

《C#中Trace.Assert的使用小结》Trace.Assert是.NET中的运行时断言检查工具,用于验证代码中的关键条件,下面就来详细的介绍一下Trace.Assert的使用,具有一定的参考价值... 目录1、 什么是 Trace.Assert?1.1 最简单的比喻1.2 基本语法2、⚡ 工作原理3