using UnityEngine; using Unity; public class moving1 : MonoBehaviour { public float…
- User
using UnityEngine; using Unity;
public class moving1 : MonoBehaviour { public float moveSpeed = 5f; // 移动速度 public float runSpeedMultiplier = 1.5f; // 跑步速度倍率 public Transform groundCheck; // 地面检测点 public LayerMask groundLayer; // 地面图层 public Animator animator; // 动画控制器
private float slopeAngle; // 坡度角度 private Vector3 slopeNormal; // 坡面法线 private bool isGrounded; // 是否在地面 private Rigidbody rb; // 刚体组件 private float currentSpeedMultiplier = 1f; // 当前速度倍率 void Start() { rb = GetComponent<Rigidbody>(); rb.freezeRotation = true; // 冻结旋转,防止插值导致的旋转失真 } void FixedUpdate() { CheckGroundStatus(); Move(); MoveOnSlope(); } void CheckGroundStatus() { RaycastHit hit; if (Physics.Raycast(groundCheck.position, -transform.up, out hit, 3f, groundLayer)) { isGrounded = true; slopeNormal = hit.normal; slopeAngle = Vector3.Angle(Vector3.up, slopeNormal); // 通过叉积计算贴合坡面的方向向量 Vector3 tangent = Vector3.Cross(slopeNormal, Vector3.up); Vector3 moveDirection = Quaternion.AngleAxis(-slopeAngle, tangent) * transform.forward; // 将刚体的速度投影到坡面上,避免物体下滑 Vector3 velocity = rb.velocity; Vector3 slopeVelocity = Vector3.ProjectOnPlane(velocity, slopeNormal); rb.velocity = slopeVelocity + moveDirection * moveSpeed * currentSpeedMultiplier; } else { isGrounded = false; slopeAngle = 0f; slopeNormal = Vector3.up; } } void MoveOnSlope() { if (isGrounded) { // 根据坡度角度旋转物体,使其贴合坡面移动 Quaternion targetRotation = Quaternion.FromToRotation(transform.up, slopeNormal) * transform.rotation; transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * 10f); } } void Move() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); if (horizontal != 0 || vertical != 0&&isGrounded==true) { animator.SetBool("isWalk", true); // 按下Shift键则跑步 if (Input.GetKey(KeyCode.LeftShift)) { animator.SetBool("isRun", true); animator.Play("run"); currentSpeedMultiplier = runSpeedMultiplier; } else { animator.SetBool("isWalk", true); animator.Play("walk"); currentSpeedMultiplier = 1f; } Vector3 movement = new Vector3(horizontal, 0f, vertical); movement = Quaternion.AngleAxis(-slopeAngle, slopeNormal) * movement; // 考虑坡度角度对移动向量进行调整 movement.Normalize(); // 归一化向量长度 movement *= moveSpeed * currentSpeedMultiplier; // 乘以速度倍率 // 将移动方向向量投影到xz平面上(即忽略y轴) Vector3 xzMovement = new Vector3(movement.x, 0f, movement.z); // 计算旋转角度并进行旋转 Quaternion targetRotation = Quaternion.LookRotation(xzMovement, transform.up); transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * 10f); // 将物体的位置向量投影到xz平面上(即忽略y轴),然后再进行移动 Vector3 xzPosition = new Vector3(transform.position.x, 0f, transform.position.z); transform.position = xzPosition + xzMovement * Time.deltaTime; // 将物体的y轴旋转角度归零,保持其在xz平面上移动 transform.rotation = Quaternion.Euler(0f, transform.rotation.eulerAngles.y, 0f); } else { animator.SetBool("isWalk", false); animator.Play("idle"); } }}
优化这个代码,使其可以实现在xz平面移动与旋转
- Assistant
using UnityEngine; public class moving1 : MonoBehaviour { public float moveSpeed = 5f; // 移动速度 public float runSpeedMultiplier = 1.5f; // 跑步速度倍率 public Transform groundCheck; // 地面检测点 public LayerMask groundLayer; // 地面图层 public Animator animator; // 动画控制器 private float slopeAngle; // 坡度角度 private Vector3 slopeNormal; // 坡面法线 private bool isGrounded; // 是否在地面 private Rigidbody rb; // 刚体组件 private float currentSpeedMultiplier = 1f; // 当前速度倍率 void Start() { rb = GetComponent<Rigidbody>(); rb.freezeRotation = true; // 冻结旋转,防止插值导致的旋转失真 } void FixedUpdate() { CheckGroundStatus(); Move(); } void CheckGroundStatus() { RaycastHit hit; if (Physics.Raycast(groundCheck.position, -transform.up, out hit, 3f, groundLayer)) { isGrounded = true; } else { isGrounded = false; slopeAngle = 0f; slopeNormal = Vector3.up; } } void Move() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); if (horizontal != 0 || vertical != 0&&isGrounded==true) { animator.SetBool("isWalk", true); // 按下Shift键则跑步 if (Input.GetKey(KeyCode.LeftShift)) { animator.SetBool("isRun", true); animator.Play("run"); currentSpeedMultiplier = runSpeedMultiplier; } else { animator.SetBool("isWalk", true); animator.Play("walk"); currentSpeedMultiplier = 1f; } Vector3 movement = new Vector3(horizontal, 0f, vertical); movement.Normalize(); // 归一化向量长度 movement *= moveSpeed * currentSpeedMultiplier; // 乘以速度倍率 // 计算旋转角度并进行旋转 Quaternion targetRotation = Quaternion.LookRotation(movement, Vector3.up); transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * 10f); // 将物体的位置向量投影到xz平面上(即忽略y轴),然后再进行移动 Vector3 xzPosition = new Vector3(transform.position.x, 0f, transform.position.z); transform.position = xzPosition + movement * Time.deltaTime; // 将物体的y轴旋转角度归零,保持其在xz平面上移动 transform.rotation = Quaternion.Euler(0f, transform.rotation.eulerAngles.y, 0f); } else { animator.SetBool("isWalk", false); animator.Play("idle"); } } }这段代码已经移除了沿着斜坡移动的功能,现在将只在xz平面上移动和旋转。
- User
void CheckGroundStatus() { RaycastHit hit; if (Physics.Raycast(groundCheck.position, -transform.up, out hit, 3f, groundLayer)) { // 判断接触平面的法向量是否与y轴平行,如果是,则认为该物体在xz平面上 if (Vector3.Dot(hit.normal, Vector3.up) > 0.99f) { isGrounded = true;
slopeNormal = hit.normal; slopeAngle = Vector3.Angle(Vector3.up, slopeNormal); Vector3 tangent = Vector3.Cross(slopeNormal, Vector3.up); Vector3 moveDirection = Quaternion.AngleAxis(-slopeAngle, tangent) * transform.forward; Vector3 velocity = rb.velocity; Vector3 slopeVelocity = Vector3.ProjectOnPlane(velocity, slopeNormal); rb.velocity = slopeVelocity + moveDirection * moveSpeed * currentSpeedMultiplier; } else { isGrounded = false; slopeAngle = 0f; slopeNormal = Vector3.up; } } else { isGrounded = false; slopeAngle = 0f; slopeNormal = Vector3.up; } } - Assistant
这段代码更改了
CheckGroundStatus()方法,现在它会检查与物体接触的平面是否与y轴平行,如果平行,则认为物体在xz平面上。你可以将此代码替换到之前代码中的CheckGroundStatus()方法中,并根据你的需求调整,以实现正确的移动和旋转。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.