unity3d 如何使Quaterion.RotateTowards()不立即旋转?

gfttwv5a  于 10个月前  发布在  其他
关注(0)|答案(2)|浏览(85)

bounty还有7天到期。回答此问题可获得+50声望奖励。dangoqut132希望引起更多关注此问题。

我没有想那么远,因为我专注于修复我的x轴鼠标夹,现在我不能让我的Quaternion.RotateTowards旋转得更慢。当我输入一个触发器时,下面的代码(在我的MouseLook/Camera脚本的Update方法中)将激活以进行不同类型的鼠标移动。

[Range(0f, 90f)][SerializeField] float xRotationLimit = 35f;

void Update()
    {
        transform.position = player.position + offset;

        if (trigger.clampRotation)
        {
            clampHorizontalRotation();
        }
        else
        {
            NormalCameraMovement();
        }
    }

void clampHorizontalRotation() 
{
Vector2 mouseDelta = Input.GetAxis(xAxis) * Vector2.right + Input.GetAxis(yAxis) * Vector2.up;

rotation.x += mouseDelta.x * sensitivity;
rotation.y += mouseDelta.y * sensitivity;

rotation.y = Mathf.Clamp(rotation.y, -yRotationLimit, yRotationLimit);

var yQuat = Quaternion.AngleAxis(rotation.y, Vector3.left);

var xQuat = Quaternion.RotateTowards(climbing.CubeObj.rotation, Quaternion.AngleAxis(rotation.x, Vector3.up), rotationSpeed * Time.deltaTime);

rotation.x = xQuat.eulerAngles.y;

transform.localRotation = yQuat;
player.transform.localRotation = xQuat;
}

void NormalCameraMovement()
{
    // Normal mouse movement goes here
}

字符串
问题是RotateTowardsvar xQuat线路。我的代码在Quaternion.RotateTowards中使用Quaternion.AngleAxis的原因是为了防止万向节锁定。但是现在当我输入“CubeObj”的触发器时,我的播放器会立即向它旋转,而不是缓慢旋转。
真实的发生的情况是:我走到我的触发对象(立方体对象),然后我的球员自动旋转缓慢向立方体对象,使我的球员面对它。从那里,我的垂直轴被钳制(这很容易),我的x轴鼠标旋转也被钳制,这样我就不能向左或向右移动超过一定程度。

jhiyze9q

jhiyze9q1#

docs:第一个月
“四元数frommaxDegreesDelta的Angular 步长朝向to旋转。[每次调用此函数时都会发生这种情况!]”
因此,您希望减小maxDegreesDelta
请记住,如果您在Update中运行此函数,则每秒可能会发生60次(假设帧速率为60)。因此,如果将maxDegreesDelta设置为1这样小的数字,您仍然可以在短短一秒钟内获得60°的旋转。考虑到整个旋转只有360°,这是相当快的!
现在,如果您刚开始构建游戏,您可能会获得高达300 FPS的帧速率。因此允许在一秒内进行最多300°的旋转,使您的旋转看起来瞬间完成。
从一个非常小的数字开始,例如0.01。现在,将其乘以Time.deltaTime,使其在设备间保持一致。然后,如果它旋转太慢,从那里调高值。将其添加为公共或序列化的浮点数,并在“播放模式”下使用检查器中的速度进行播放,直到您满意为止。记下“right”值,然后在您 * 未 * 处于PlayMode时应用该值。

wecizke3

wecizke32#

我想这条线是错的

var xQuat = Quaternion.RotateTowards(climbing.CubeObj.rotation, Quaternion.AngleAxis(rotation.x, Vector3.up), rotationSpeed * Time.deltaTime);

字符串
你在这里得到一个从climbing.rotationQuaternion.AngleAxis(rotation.x, Vector3.up)的旋转。由于climbing.CubeObj.rotation不会改变,因此,假设鼠标没有移动,这将总是或多或少相同的旋转。
=>您在此处没有平滑地旋转到目标旋转!仅平滑目标方向本身,但仍会立即捕捉到该方向。
如果我理解正确的话,你实际上想要的是将你绕X轴的旋转固定在climbing.CubeObj的旋转周围35°。
然后你更希望平稳地旋转你的球员到这个目标旋转。
所以你的目标旋转应该是这样的。

rotation.x = Mathf.Clamp(rotation.x, -xRotationLimit, xRotationLimit);
// This is now a rotation within 35 degrees from the climbing.CubeObj.rotation
var xQuat = climbing.CubeObj.rotation * Quaternion.AngleAxis(rotation.x, Vector3.up);


然后

// In general: careful with mixing local and global rotations
// this now smoothly rotates the player towards the target rotation 
player.transform.rotation = Quaternion.RotateTowards(player.transform.rotation, xQuat, rotationSpeed * Time.deltaTime);

相关问题