Unity万向节死锁解决方案(2023/12/4)

1.万向节死锁无法解决,这是因为它的特性就是如此。就像玻璃杯就是玻璃,这不可否认。(别钻牛角尖昂)

2.大多数情况下欧拉角足够用,例如(CF---摄像机不可能绕z轴旋转,x轴旋转也不会超过九十度,因为那样人物的腰子会被扭断。塔防游戏---保卫萝卜。吃鸡---第三人称视角欧拉角足够)。

3.如果是太空类游戏,为了自由感必须对摄像机视角进行无限滑移,即在任意方向上都能够按照正常思维逻辑进行旋转。则欧拉角不适用,应使用四元数或其他方案。

4.四元数解决方案:

最重要的一句:transform.localRotation *= Quaternion.Euler(freeMove_Mouse);

将以下脚本给予摄像机即可。

鼠标控制屏幕旋转,QE控制z轴旋转,WASD控制人物移动。

Unity万向节死锁解决方案_哔哩哔哩_bilibili

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Text : MonoBehaviour
{
    Vector3 freeMove_Mouse = new Vector3(0, 0, 0);      //存放鼠标移动信息
    Vector3 freeMove_Position = new Vector3(0, 0, 0);   //存放位置移动信息
    void Update()
    {
        //旋转
        if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
        {
               freeMove_Mouse.y += Input.GetAxis("Mouse X");
               freeMove_Mouse.x -= Input.GetAxis("Mouse Y");
        }
        if (Input.GetKey(KeyCode.Q))
        {
            freeMove_Mouse.z += 0.2f;
        }
        if (Input.GetKey(KeyCode.E))
        {
            freeMove_Mouse.z -= 0.2f;
        }
        transform.localRotation *= Quaternion.Euler(freeMove_Mouse);
        freeMove_Mouse = Vector3.zero;


        //不重要
        if(Input.GetAxis("Vertical")!=0 || Input.GetAxis("Horizontal") != 0)
        {
            freeMove_Position.z += Input.GetAxis("Vertical");
            freeMove_Position.x += Input.GetAxis("Horizontal");
        }
        transform.Translate(freeMove_Position*Time.deltaTime*5);
        freeMove_Position = Vector3.zero;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

焦虑的狼堡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值