用户控制的四元数勒吗? [英] User controlled Quaternion Slerp?

查看:103
本文介绍了用户控制的四元数勒吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过用户控制来旋转球体.

I am trying to rotate a sphere with user control.

void RotateGlobe() {
    SetLock();

    if (Input.GetMouseButton(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        userInput.x = Input.GetAxis("Mouse X");
        userInput.y = Input.GetAxis("Mouse Y");

        if (userInput.magnitude > 0 & Physics.Raycast(ray, Mathf.Infinity, LayerMask.NameToLayer("UI")))
        {
            Vector2 movement = new Vector2(Mathf.Sign(userInput.x), Mathf.Sign(userInput.y));

            globe.transform.rotation = 
                Quaternion.Slerp(globe.transform.rotation, 
                Quaternion.Euler(0, movement.y * 90, movement.x * 180 + globe.transform.rotation.eulerAngles.x), 
                Time.time * speed);
        }
    }

    RemoveLock();
}

我不太了解四元数(然后又是真正的四元数),但是地球不仅不能正确响应用户输入,而且还卡在x轴上.我知道不移动可能是从180到-180的最短路径.

I don't quite understand Quaternions (then again, who really does), but not only is the globe not responding correctly to user input, it is getting stuck on the x-axis. I get that it's probably taking the shortest path from 180 to -180 by not moving.

我想在这一点上,我完全愿意更改此代码,以使用更好的方法(而不是slerp),但是我完全陷入了困境,以前的方法同样困难而混乱.

I guess at this point, I'm completely willing to change this code in favor of a better method (not slerp), but I am completely stuck and previous methods were equally difficult and choppy.

您如何建议我开始旋转这个球体?

How would you suggest I go about rotating this sphere?

作为奖励,如果我能约束一个轴,那我们会很棒.我真的不想旋转超过90或-90.

As a bonus, if I could constrain an axis that would we great. I don't really want to spin further than 90 or -90.

推荐答案

简单的解决方案-您可以使用Unity的RotateAround,这是他们最有用的调用之一

easy solution - you can use Unity's RotateAround which is one of their most useful calls

http://docs.unity3d.com/ScriptReference/Transform.RotateAround.html

请注意,网上有一些令人困惑的评论,其中不推荐使用RotateAround;实际上,只有一个旧版本已被弃用.这是您在Unity中使用的最常见的调用之一.享受吧!

Note that there are some confusing comments online that RotateAround is deprecated; in fact it's only one old version that is deprecated. It's one of the most common calls you use in Unity. Enjoy!

第二次.

请注意,您实际上实际上是在计算欧拉角!或可能的localEulerAngles. 您可以在它们之间简单地绑住!!!!

Note that you are indeed actually calculating the euler angles! or possible localEulerAngles. you can simply slerp between them!!!!

类似....

Vector3 fromEulers = blah;
Vector3 toEulers = blah;

transform.localEulerAngles = .. slerp .. fromEulers toEulers;

最后.请注意,类似lerp"功能具有与 MoveTowards 类似的功能按自己的步骤".

Finally. note that the "lerp -like" functions have equivalents like MoveTowards which you can use "in your own step".

SmoothStep 实际上是关于所有单位中最有用的呼叫!

SmoothStep is actually about the most useful call in all of unity!

这篇关于用户控制的四元数勒吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆