User controlled Quaternion Slerp?

左心房为你撑大大i 提交于 2020-01-05 07:25:27

问题


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();
}

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.

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?

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.


回答1:


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

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

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!

secondly.

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

something like....

Vector3 fromEulers = blah;
Vector3 toEulers = blah;

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

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

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



来源:https://stackoverflow.com/questions/36724786/user-controlled-quaternion-slerp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!