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

【Unity】使用Cinemachine+CharacterController实现第三人称视角下的角色视角、移动和跳跃控制

1.初始配置

  • 安装Cinemachine插件
  • 给角色添加CharacterConroller
  • 创建Cinemachine-->Free Look Camera
  • 在Free Look Camera中调整参数,Y Axis勾选Inver,X Axis取消勾选Inver
  • Free Look Camera要看向角色 + 跟随角色(自行设置,我就不再赘述了)

2.编写C#脚本

代码可以自行收藏直接用,关键自行调整的参数是:

speed、rotateSpeed、jumpHeight

using UnityEngine;public class PlayerController : MonoBehaviour
{[Header("参数")] public float speed; //角色移动速度(跑步的时候是两倍)public float rotateSpeed; //角色转向速度public float playerGravity = 9.8F; //角色重力public float jumpForce; //跳跃力度public float jumpHeight = 2F; //跳跃高度public float verticalVelocity = 0;//垂直速度[Header("状态")] public bool isMove; //是否在移动public bool isRun; //是否在跑步[Header("输入")]public Vector3 inputDirection; //wasd输入(x值表示horizontal,z值表示vertical,y值为0)[Header("组件")]public CharacterController characterController; //这个在Start函数中初始化,你也可以在Inspector初始化private void Start(){inputDirection = new Vector3();characterController = GetComponent<CharacterController>();}private void Update(){//计算WASD输入inputDirection.x = Input.GetAxisRaw("Horizontal");inputDirection.z = Input.GetAxisRaw("Vertical");//获得是否是移动和跑步状态isMove = inputDirection.magnitude > 0;isRun = isMove && Input.GetKey(KeyCode.LeftShift);//光标的可见性设置if (Input.GetKeyDown(KeyCode.I)){Cursor.visible = !Cursor.visible;}if (Input.GetKeyDown(KeyCode.Space)){characterController.SimpleMove(transform.up * jumpForce);}if (!isMove) return;//实现角色转向到相机平行于xz轴构成的平面的朝向Vector3 targetDirection = new Vector3(inputDirection.x, 0, inputDirection.z);float y = Camera.main.transform.rotation.eulerAngles.y;Quaternion targetRotation = Quaternion.Euler(0, y, 0) * Quaternion.LookRotation(targetDirection);// 使用Quaternion.Slerp进行平滑转向transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotateSpeed);}private void FixedUpdate(){Vector3 moveDirection = Vector3.zero;moveDirection += transform.forward * inputDirection.z;moveDirection += transform.right * inputDirection.x;moveDirection *= Time.fixedDeltaTime * (isRun ? speed*2 : speed);// 重力if (!characterController.isGrounded)verticalVelocity -= playerGravity * Time.fixedDeltaTime;// a*t=velseverticalVelocity = 0.0F;// 跳跃if (Input.GetButton("Jump")){// 得在地面才能跳if (characterController.isGrounded){verticalVelocity = Mathf.Sqrt(jumpHeight * 2 / playerGravity) * playerGravity;// 初始向上速度}}moveDirection.y += verticalVelocity * Time.fixedDeltaTime;// v*t=s// 移动characterController.Move(moveDirection);}
}

3.测试效果

测试运行效果,看看是否正常

http://www.xdnf.cn/news/220.html

相关文章:

  • Maven与Springboot创建
  • Qt QThread 两种线程管理方法
  • 通信算法之269 : OFDM信号的循环自相关特性用于无人机图传信号识别
  • Ethernet/IP转ProfiNet边缘计算网关在能源管理中的应用:跨系统数据聚合与智能分析
  • (7)VTK C++开发示例 --- 使用交互器
  • 英伟达A100、H100、H800性能详细对比
  • 4.18日学习--引用
  • 记一次bat脚本的坑:中文注释导致脚本不能运行
  • 【无标题】作业
  • 不确定与非单调推理的基本概念
  • 新书速览|DeepSeek移动端AI应用开发:基于Android与iOS
  • win11系统截图的几种方式
  • SQL通用语法和注释,SQL语句分类(DDL,DML,DQL,DCL)及案例
  • 深入简出:KL散度、交叉熵、熵、信息量简介、交叉熵损失
  • Spring Boot自动配置原理深度解析:从条件注解到spring.factories
  • FFmpeg 硬核指南:从底层架构到播放器全链路开发实战 基础
  • Animated Raindrop Ripples In HLSL
  • 关于使用webpack构建的vue项目,如何使用windicss
  • Superduper - 在数据上构建端到端AI工作流和应用
  • Java面试中问单例模式如何回答
  • 我的gittee仓库
  • LLaMA Factory多模态微调实践:微调Qwen2-VL构建文旅大模型
  • 【国家能源集团生态协作平台-注册/登录安全分析报告】
  • 操作系统 第四章 文件管理
  • 施磊老师基于muduo网络库的集群聊天服务器(二)
  • 十天借助 Trae 实现 “幸运塔塔屋” 小程序时光记忆功能之旅
  • Xcode16 调整 Provisioning Profiles 目录导致证书查不到
  • 多模态记忆融合:基于LSTM的连续场景生成——突破AI视频生成长度限制
  • Orgin为柱状图加趋势线
  • 零基础上手Python数据分析 (17):[案例实战] 电商销售数据分析 - 从数据到洞察的全流程演练