行者己度 2019-03-04 11:41
浏览 418

transform固定轴旋转问题

是这样的,我想实现根据按键(wasd),使方块根据对应的轴缓慢旋转90度(xy)。

但是在实现过程中,发现旋转总是会发生奇怪的错误,貌似这个问题是因为万向锁造成的,但是我不清楚该如何解决.

以下是问题代码,不知道该如何修改:

void Update()
{

    //获取按键方向
    if (Input.GetKeyDown(KeyCode.D))
        direction = Direction.Right;
    else if (Input.GetKeyDown(KeyCode.A))
        direction = Direction.Left;
    else if (Input.GetKeyDown(KeyCode.W))
        direction = Direction.Up;
    else if (Input.GetKeyDown(KeyCode.S))
        direction = Direction.Down;

    //更新目标旋转角度
    if (Input.anyKeyDown)
    {
        if (direction == Direction.Up)
            rotate = Quaternion.Euler(rotate.eulerAngles.x + -90, rotate.eulerAngles.y, rotate.eulerAngles.z);
        else if (direction == Direction.Down)
            rotate = Quaternion.Euler(rotate.eulerAngles.x + 90, rotate.eulerAngles.y, rotate.eulerAngles.z);
        else if (direction == Direction.Left)
            rotate = Quaternion.Euler(rotate.eulerAngles.x, rotate.eulerAngles.y + -90, rotate.eulerAngles.z);
        else if (direction == Direction.Right)
            rotate = Quaternion.Euler(rotate.eulerAngles.x, rotate.eulerAngles.y + 90, rotate.eulerAngles.z);
    }

    //实时刷新旋转角度
    transform.rotation = Quaternion.RotateTowards(transform.rotation, rotate, 2f);
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题
    • ¥15 Visual Studio问题
    • ¥20 求一个html代码,有偿