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平面移动与旋转