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

【unity实战】使用新版输入系统Input System+Rigidbody实现第三人称人物控制器(附项目源码)

最终效果

在这里插入图片描述

前言

使用CharacterController实现3d角色控制器,之前已经做过很多了:
【unity小技巧】unity最完美的CharacterController 3d角色控制器,实现移动、跳跃、下蹲、奔跑、上下坡、物理碰撞效果,复制粘贴即用
【unity实战】Cinemachine虚拟相机+Character Controller实现俯视角、第三人称角色控制,复制粘贴即用

有的人就会问了,使用Rigidbody要怎么做呢?这不就来了,本文主要是使用新版输入系统Input System+Rigidbody实现第三人称人物控制器,我就不做特别复杂了,其他内容欢迎大家自行补充。因为我也不是很推荐大家使用Rigidbody,CharacterController 其实已经可以满足我们开发中的所有需求了。Rigidbody定义一些CharacterController自带的功能真的非常麻烦,比如爬坡,走楼梯等等,所以我这里主要只是带大家了解一下,并不会深入研究。

使用Input System获取玩家输入

参考:【推荐100个unity插件之18】Unity 新版输入系统Input System的使用,看这篇就够了

我这里直接使用Player Input组件,生成的默认的Input Actions映射
在这里插入图片描述
新增脚本获取玩家输入

/// <summary>
/// 玩家输入
/// </summary>
public class PlayerInput : MonoBehaviour
{// 用于存储移动输入的向量public Vector2 MoveInput { get; private set; }// 用于存储视角输入的向量public Vector2 LookInput { get; private set; }public bool ChangeCameraWasPressedThisFrame{get; private set; }//是否按下切换相机private InputActions _input;private void OnEnable(){_input = new InputActions();_input.Player.Enable();_input.Player.Move.performed += SetMove;_input.Player.Move.canceled += SetMove;_input.Player.Look.performed += SetLook;_input.Player.Look.canceled += SetLook;}private void OnDisable(){_input.Player.Move.performed -= SetMove;_input.Player.Move.canceled -= SetMove;_input.Player.Look.performed -= SetLook;_input.Player.Look.canceled -= SetLook;_input.Player.Disable();  }private void SetMove(InputAction.CallbackContext context){MoveInput = context.ReadValue<Vector2>();}private void SetLook(InputAction.CallbackContext context){LookInput = context.ReadValue<Vector2>();}
}

人物添加刚体

添加刚体,配置参数
在这里插入图片描述

控制角色移动

新增脚本控制角色移动,对这里的AddRelativeForce不太了解的小伙伴可以查看我这篇文章:
【unity小技巧】常用的方法属性和技巧汇总(长期更新)
在这里插入图片描述

public class PlayerController : PlayerInput
{Rigidbody _rb;[Header("移动")][SerializeField] float _speed= 1000f;// 移动的速度private void Awake(){_rb = GetComponent<Rigidbody>();}private void FixedUpdate(){PlayerMove();}// 计算并应用玩家的移动private void PlayerMove(){// 根据输入和速度计算移动向量_playerMoveInput = new Vector3(MoveInput.x, 0, MoveInput.y).normalized * _speed;// 将相对力应用到刚体上_rb.AddRelativeForce(_playerMoveInput, ForceMode.Force);}
}

配置
在这里插入图片描述

效果
在这里插入图片描述

手搓代码控制相机视角

修改PlayerInput

[Header("相机视角控制")]
public Transform CameraFollow;// 用于跟随摄像机的 Transform
private Vector3 _playerLookInput;// 玩家视角输入
private float _playerRotation;// 角色旋转角度
private float _cameraPitch;// 摄像机俯仰角度
[SerializeField] float _rotationSpeed = 180.0f;// 角色旋转速度
[SerializeField] float _pitchSpeed = 180.0f;// 摄像机俯仰速度private void Awake()
{_rb = GetComponent<Rigidbody>();mainCamera = Camera.main; // 获取主相机
}private void Update()
{_playerLookInput = new Vector3(LookInput.x, -LookInput.y, 0f) * Time.deltaTime;// 获取视角输入PlayerLook(); // 更新角色的旋转PitchCamera(); // 更新摄像机的俯仰角度
}// 更新角色的旋转
private void PlayerLook()
{_playerRotation += _playerLookInput.x * _rotationSpeed;_rb.rotation = Quaternion.Euler(0f, _playerRotation, 0f);
}// 更新摄像机的俯仰角度
private void PitchCamera()
{Vector3 rotationValues = CameraFollow.rotation.eulerAngles;_cameraPitch += _playerLookInput.y * _pitchSpeed;_cameraPitch = Mathf.Clamp(_cameraPitch, -89.9f, 89.9f);//限制俯仰视角角度CameraFollow.rotation = Quaternion.Euler(_cameraPitch, rotationValues.y, rotationValues.z);
}

配置相机为角色的子物体
在这里插入图片描述
效果
在这里插入图片描述

最终代码

using UnityEngine;public class PlayerController : PlayerInput
{Rigidbody _rb;[Header("移动")]Vector3 _playerMoveInput;// 玩家移动向量[SerializeField] float _speed = 1000f;// 移动的速度[Header("相机视角控制")]public Transform CameraFollow;// 用于跟随摄像机的 Transformsprivate Vector3 _playerLookInput;// 玩家视角输入private float _playerRotation;// 角色旋转角度private float _cameraPitch;// 摄像机俯仰角度[SerializeField] float _rotationSpeed = 180.0f;// 角色旋转速度[SerializeField] float _pitchSpeed = 180.0f;// 摄像机俯仰速度private Camera mainCamera; // 主相机private void Awake(){_rb = GetComponent<Rigidbody>();mainCamera = Camera.main; // 获取主相机}private void Update(){_playerLookInput = new Vector3(LookInput.x, -LookInput.y, 0f) * Time.deltaTime;// 获取视角输入PlayerLook(); // 更新角色的旋转PitchCamera(); // 更新摄像机的俯仰角度}private void FixedUpdate(){PlayerMove();}// 计算并应用玩家的移动private void PlayerMove(){// 根据输入和速度计算移动向量_playerMoveInput = new Vector3(MoveInput.x, 0, MoveInput.y).normalized * _speed;// 将相对力应用到刚体上_rb.AddRelativeForce(_playerMoveInput, ForceMode.Force);}// 更新角色的旋转private void PlayerLook(){_playerRotation += _playerLookInput.x * _rotationSpeed;_rb.rotation = Quaternion.Euler(0f, _playerRotation, 0f);}// 更新摄像机的俯仰角度private void PitchCamera(){Vector3 rotationValues = CameraFollow.rotation.eulerAngles;_cameraPitch += _playerLookInput.y * _pitchSpeed;_cameraPitch = Mathf.Clamp(_cameraPitch, -89.9f, 89.9f);//限制俯仰视角角度CameraFollow.rotation = Quaternion.Euler(_cameraPitch, rotationValues.y, rotationValues.z);}
}

源码

很遗憾源码我并不想免费分享,我也建议大家能自己手动去敲代码,逐步实现和理解每一块功能。项目实现所涉及的主要功能思路和代码我也已经毫无保留的分享在文章中了,当然,如果你真的需要的话,源码我也放出来了,收个辛苦费,就当作你对我不断创作的支持。力量随微,心暖人。您的每一次支持都是我创作的最大动力!!!

https://gf.bilibili.com/item/detail/1106435120

完结

赠人玫瑰,手有余香!如果文章内容对你有所帮助,请不要吝啬你的点赞评论和关注,你的每一次支持都是我不断创作的最大动力。当然如果你发现了文章中存在错误或者有更好的解决方法,也欢迎评论私信告诉我哦!

好了,我是向宇,https://xiangyu.blog.csdn.net

一位在小公司默默奋斗的开发者,闲暇之余,边学习边记录分享,站在巨人的肩膀上,通过学习前辈们的经验总是会给我很多帮助和启发!如果你遇到任何问题,也欢迎你评论私信或者加群找我, 虽然有些问题我也不一定会,但是我会查阅各方资料,争取给出最好的建议,希望可以帮助更多想学编程的人,共勉~
在这里插入图片描述

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • python网络爬虫(一)——网络爬虫基本原理
  • 全国大学生数据建模比赛——深度学习
  • ROS Simulink 支持与限制
  • python实战一:合并多个Excel中数据
  • ios私钥证书(p12)导入失败,Windows OpenSSl 1.1.1 下载
  • iptables防火墙常用命令,运维必备
  • 【网络原理】Udp 的报文结构,保姆式教学,快速入门
  • Transformer简明笔记:文本翻译
  • 充气泵芯片|充气泵方案芯片SIC8833
  • 【专题】2024年中国游戏出海洞察报告合集PDF分享(附原数据表)
  • Redis的String和Hash
  • 网络-多路io
  • Java基于微信小程序的美食推荐小程序,附源码
  • 基于InstaSPIN-user's guide Foc电流环速度环PI参数计算过程
  • 2024年四款必备的Windows录屏工具推荐!
  • 《微软的软件测试之道》成书始末、出版宣告、补充致谢名单及相关信息
  • 【编码】-360实习笔试编程题(二)-2016.03.29
  • 【划重点】MySQL技术内幕:InnoDB存储引擎
  • Angular js 常用指令ng-if、ng-class、ng-option、ng-value、ng-click是如何使用的?
  • C++11: atomic 头文件
  • centos安装java运行环境jdk+tomcat
  • ES6系统学习----从Apollo Client看解构赋值
  • Java 内存分配及垃圾回收机制初探
  • JavaScript 基本功--面试宝典
  • JavaScript设计模式系列一:工厂模式
  • Java多态
  • Js基础知识(一) - 变量
  • js算法-归并排序(merge_sort)
  • linux学习笔记
  • Magento 1.x 中文订单打印乱码
  • Phpstorm怎样批量删除空行?
  • RedisSerializer之JdkSerializationRedisSerializer分析
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • socket.io+express实现聊天室的思考(三)
  • Theano - 导数
  • Twitter赢在开放,三年创造奇迹
  • 不发不行!Netty集成文字图片聊天室外加TCP/IP软硬件通信
  • 从setTimeout-setInterval看JS线程
  • 三分钟教你同步 Visual Studio Code 设置
  • 小程序测试方案初探
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • 优化 Vue 项目编译文件大小
  • PostgreSQL 快速给指定表每个字段创建索引 - 1
  • puppet连载22:define用法
  • 没有任何编程基础可以直接学习python语言吗?学会后能够做什么? ...
  • ​七周四次课(5月9日)iptables filter表案例、iptables nat表应用
  • #### go map 底层结构 ####
  • #if #elif #endif
  • (21)起落架/可伸缩相机支架
  • (23)Linux的软硬连接
  • (day6) 319. 灯泡开关
  • (ibm)Java 语言的 XPath API
  • (ros//EnvironmentVariables)ros环境变量
  • (力扣记录)1448. 统计二叉树中好节点的数目
  • (一)VirtualBox安装增强功能