【发布时间】:2019-07-06 04:31:09
【问题描述】:
我正在尝试围绕 X、Y 和 Z 轴旋转播放器。 Y 轴不应从最后一个角度移动。例如,如果我向左旋转 45 度,播放器不应该旋转回 0。播放器 X 和 Z 轴最多旋转 30 度,然后当 Input 不再使用时,设置为 0。
通过反复试验,我终于让我的 Y 角不 Slerp 回到 0。但是,X 和 Z,仍然认为 Y 是 0 度。玩家旋转(假设向左 45 度),但沿 X 和 Z 移动就像 Y 是 0 度。
我一直在阅读文章和主题,并观看多个领域的视频,包括但不限于 StackOverflow、Unity 论坛、Unity API 和 YouTube 视频。
Video of Current Game - 注意引擎排气 - X 和 Z 永远不会更改为摄像机视图/玩家 Y 方向的新法线。
void Update()
{
if(!controller.isGrounded)
{
//Three degree's
moveDirection = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Thrust"), Input.GetAxis("Vertical"));
moveDirection *= speed;
//rotate around Y-Axis
transform.Rotate(0, Input.GetAxis("Yaw") * rotationSpeed, 0);
float currentY = transform.eulerAngles.y; //save Y for later
//rotation around X and Z
float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
float tiltAroundZ = -1 * (Input.GetAxis("Horizontal") * tiltAngle);
Quaternion targetRotation = Quaternion.Euler(tiltAroundX, currentY, tiltAroundZ);
Vector3 finalRotation = Quaternion.Slerp(transform.rotation, targetRotation, smooth).eulerAngles;
finalRotation.y = currentY; //reintroduce Y
transform.rotation = Quaternion.Euler(finalRotation);
controller.Move(moveDirection * Time.deltaTime);
}
【问题讨论】: