Luncher_午餐 2023-02-16 11:27 采纳率: 87%
浏览 46
已结题

Unity角色控制器移动斜坡自动下滑

角色控制器移动玩家上斜坡卡在坡上的问题怎么解决,代码如下

CharacterController playerController;


    Vector3 direction;


    public float speed = 5;
    public float Runspeed = 10;
    public float jumpPower = 5;
    public float gravity = 7f;


    public float mousespeed = 5f;


    public float minmouseY = -45f;
    public float maxmouseY = 45f;


    float RotationY = 0f;
    float RotationX = 0f;


    public Transform agretctCamera;


    // Use this for initialization
    void Start()
    {
        playerController = this.GetComponent<CharacterController>();
        
        
    }

    // Update is called once per frame
    void Update()
    {
        //获取世界按键方向
        float _horizontal = Input.GetAxis("Horizontal");
        float _vertical = Input.GetAxis("Vertical");

        if (playerController.isGrounded)//跳跃
        {
            direction = new Vector3(_horizontal, 0, _vertical);
            if (Input.GetKeyDown(KeyCode.Space))
                direction.y = jumpPower;
        }
        direction.y -= gravity * Time.deltaTime;
        playerController.Move(playerController.transform.TransformDirection(direction * Time.deltaTime * speed));//移动

        RotationX += agretctCamera.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * mousespeed;//视觉转动
        RotationY -= Input.GetAxis("Mouse Y") * mousespeed;
        RotationY = Mathf.Clamp(RotationY, minmouseY, maxmouseY);
        this.transform.eulerAngles = new Vector3(0, RotationX, 0);
        agretctCamera.transform.eulerAngles = new Vector3(RotationY, RotationX, 0);
        //疾跑
        if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W))
        {
            playerController.Move(playerController.transform.TransformDirection(direction * Time.deltaTime * Runspeed));
            jumpPower = 2f;
            Camera.main.fieldOfView = 70;
            GameObject.Find("Canvas/状态/奔跑特效").SetActive(true);
            GameObject.Find("Me/Main Camera/粒子特效").SetActive(true);
        }
        else
        {
            jumpPower = 3f;
            Camera.main.fieldOfView = 60;
            GameObject.Find("Canvas/状态/奔跑特效").SetActive(false);
            GameObject.Find("Me/Main Camera/粒子特效").SetActive(false);
            //摇杆控制移动
            Vector3 dir = new Vector3(JoyStick.h, 0, JoyStick.v);//获取摇杆脚本方向
            if (dir.sqrMagnitude > 0)
            {
                playerController.Move(playerController.transform.TransformDirection(dir * Time.deltaTime * speed));
            }

        }
  • 写回答

2条回答 默认 最新

  • 「已注销」 2023-02-16 19:14
    关注

    角色控制器在上斜坡时卡住的问题通常可以通过调整CharacterController的stepOffset属性来解决。stepOffset表示跨越障碍物的高度,如果角色控制器在斜坡上卡住了,可以试着将stepOffset属性的值调大一点,例如:

    playerController.stepOffset = 0.3f;
    

    同时,为了确保角色控制器在上下坡时移动的流畅性,可以在代码中将CharacterController的slopeLimit属性设置为一个适当的值,例如:

    playerController.slopeLimit = 45f;
    

    这样可以确保角色控制器在45度以内的斜坡上可以正常移动。
    如果对您有帮助,请给与采纳,谢谢。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月25日
  • 已采纳回答 2月17日
  • 创建了问题 2月16日

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败