许可优化
产品
解决方案
服务支持
关于
软件库
当前位置:服务支持 >  软件文章 >  Unity解决:3D开发模式第三人称视角 WASD控制角色移动旋转 使用InputSystem

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

阅读数 4
点赞 0
article_banner

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

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class InputHander : MonoBehaviour
  5. {
  6. public float horizontal;
  7. public float vertical;
  8. public float moveAmount;
  9. public float mouseX;
  10. public float mouseY;
  11. PlayerControls inputActions;//声明 InputSystem的脚本对象
  12. Vector2 movementInput;//存储 WASD输入的值
  13. public void OnEnable()
  14. {
  15. //获取设备上的输入
  16. if (inputActions == null)
  17. {
  18. inputActions = new PlayerControls();
  19. //绑定输入的值
  20. inputActions.PlayerMovement.MovementAction.performed += outputActions => movementInput = outputActions.ReadValue<Vector2>();
  21. }
  22. inputActions.Enable();//启用
  23. }
  24. public void OnDisable()
  25. {
  26. inputActions.Disable();//禁用
  27. }
  28. public void TickInputt(float delta)
  29. {
  30. MoveInput(delta);
  31. }
  32. public void MoveInput(float delta) {
  33. horizontal = movementInput.x;
  34. vertical = movementInput.y;
  35. moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
  36. }
  37. }
cs
运行

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

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerLocomotion : MonoBehaviour
  5. {
  6. InputHander inputHander;
  7. Vector3 moveDirection;
  8. public CapsuleCollider capsuleCollider;
  9. public new Rigidbody rigidbody;
  10. Vector2 movementInput;//存储 WASD输入的值
  11. Transform cameraObject;
  12. [HideInInspector]
  13. public Transform myTransform;
  14. [Header("Stats")]
  15. [SerializeField]
  16. float movementSpeed = 5;
  17. [SerializeField]
  18. float rotationSpeed = 10;
  19. // Start is called before the first frame update
  20. void Start()
  21. {
  22. rigidbody = GetComponent<Rigidbody>();
  23. if (rigidbody==null)
  24. {
  25. rigidbody = gameObject.AddComponent<Rigidbody>();
  26. rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
  27. }
  28. capsuleCollider = GetComponent<CapsuleCollider>();
  29. if (capsuleCollider==null)
  30. {
  31. capsuleCollider = gameObject.AddComponent<CapsuleCollider>();
  32. capsuleCollider.center = new Vector3(0, 1, 0);
  33. capsuleCollider.radius = 0.5f;
  34. capsuleCollider.height = 2;
  35. capsuleCollider.direction = 1;
  36. }
  37. inputHander = GetComponent<InputHander>();
  38. cameraObject = Camera.main.transform;
  39. myTransform = transform;
  40. }
  41. // Update is called once per frame
  42. void Update()
  43. {
  44. float delta = Time.deltaTime;
  45. inputHander.TickInputt(delta);
  46. moveDirection = cameraObject.forward * inputHander.vertical;
  47. moveDirection += cameraObject.right * inputHander.horizontal;
  48. moveDirection.Normalize();
  49. float speed = movementSpeed;
  50. moveDirection *= speed;
  51. //移动
  52. Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, new Vector3(0, 0, 0));
  53. rigidbody.velocity = projectedVelocity;
  54. HandleRotation(delta);
  55. }
  56. //转向
  57. private void HandleRotation(float delta) {
  58. Vector3 targerDir = Vector3.zero;
  59. targerDir = cameraObject.forward * inputHander.vertical;
  60. targerDir += cameraObject.right * inputHander.horizontal;
  61. targerDir.Normalize();
  62. targerDir.y = 0;
  63. if (targerDir == Vector3.zero)
  64. {
  65. targerDir = myTransform.forward;
  66. }
  67. Quaternion tr = Quaternion.LookRotation(targerDir);
  68. Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rotationSpeed * delta);
  69. myTransform.rotation = targetRotation;
  70. }
  71. }
cs
运行

完成


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删
相关文章
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空