是初学者的一些尝试。开始信心满满的学习UNITY了先把准备工作准备好专门分个磁盘出来做Unity。第一步就中道崩殂了······E盘为什么说有不可移动的东西碎片整理也不行算了干脆卸了重装吧就当清理内存了。怎么搞了一整个上午。吃完午饭磁盘也搞好了开始下载吧。还分国内版和国外版啊真好不用自己找镜像了。编辑器推荐的是VS没怎么用过IDEA用的比较多。都下好了。太棒了可以迈出第一步了。中文社区还有教程哇好贴心。时间好长啊还是先自己摸索摸索吧。嗯学会了建地面和模型AI给的代码也跑动了但那个黑球为什么是材料单纯的图标吗已经很晚了明天再仔细研究研究代码吧。using UnityEngine; public class PlayerMove : MonoBehaviour { public float moveSpeed 5f; public float rollSpeed 180f; // 翻滚速度度数/秒可在Inspector调整 private Rigidbody rb; private Quaternion targetRotation; // 目标旋转角度 void Start() { // 获取/自动添加刚体 rb GetComponentRigidbody(); if (rb null) { rb gameObject.AddComponentRigidbody(); } rb.freezeRotation false; // 取消旋转锁定才能翻滚 rb.useGravity true; rb.isKinematic false; // 初始旋转为当前角度 targetRotation transform.rotation; } void Update() { // 1. 基础移动逻辑 float h Input.GetAxis(Horizontal); // A/D 左右 float v Input.GetAxis(Vertical); // W/S 前后 Vector3 moveDir Vector3.right * h Vector3.forward * v; if (moveDir.magnitude 1f) { moveDir.Normalize(); } // 应用移动 if (moveDir.magnitude 0.1f) { rb.velocity new Vector3(moveDir.x * moveSpeed, rb.velocity.y, moveDir.z * moveSpeed); // 2. 核心计算翻滚旋转角度 // 向左/右移 → 绕Z轴翻滚向前/后移 → 绕X轴翻滚 float rollX -v * rollSpeed * Time.deltaTime; // 前后移 → X轴翻滚W前滚S后滚 float rollZ h * rollSpeed * Time.deltaTime; // 左右移 → Z轴翻滚A左滚D右滚 // 累加旋转角度基于本地坐标系 targetRotation * Quaternion.Euler(rollX, 0, rollZ); // 平滑插值到目标旋转避免翻滚太生硬 transform.rotation Quaternion.Lerp(transform.rotation, targetRotation, 0.1f); } else { // 停止移动时缓慢恢复到初始旋转可选根据需求保留 targetRotation Quaternion.identity; // 恢复到无旋转状态 transform.rotation Quaternion.Lerp(transform.rotation, targetRotation, 0.05f); // 停止移动时减速可选 rb.velocity Vector3.Lerp(rb.velocity, Vector3.zero, 0.1f); } } }