简体   繁体   English

向摄像机旋转超过360度的方向添加一个整数

[英]Adding an integer to the rotation of a camera past 360 degrees

What i am doing here is rotating the camera by 90 degrees at a time on keypress, using 我在这里所做的是在每次按下按键时将相机旋转90度

currentpos = transform.eulerAngles + new Vector3 (0, 90, 0);

and

transform.eulerAngles = Vector3.Lerp (transform.eulerAngles, currentpos, Time.deltaTime * speed); 

the problem is when the camera reaches 360 degrees it wants to try change the angle to 450 so gets stuck in an infinite rotating loop. 问题是,当摄像机达到360度时,它想尝试将角度更改为450度,从而陷入无限旋转的循环中。 i have to use euler angles because the camera angled 45 degrees and euler angles rotates globally rather than locally 我必须使用欧拉角,因为相机成45度角且欧拉角会全局旋转,而不是局部旋转

My question is how do i make it stop the infinite loop and go from 360 degrees to 90 degrees as well as not rotating in the opposite for example i tried 我的问题是我如何使其停止无限循环并从360度变为90度,以及不反向旋转(例如我尝试过的)

if (transform.eulerAngles.y == 360){
        currentpos = transform.eulerAngles + new Vector3 (0, 0, 0);
    }

however Vector3.Lerp then rotates in the opposite direction back toward 0 rather than going through 360 degrees to reach 0. 但是, Vector3.Lerp然后沿相反的方向旋转回0,而不是经过360度到达0。

I suggest using Mathf.LerpAngle() 我建议使用Mathf.LerpAngle()

Whether you set an angle more than 360 (say 360 + x) or just an angle x, it will interpolate as you would expect without trying to shoot in the opposite direction. 无论您将角度设置为大于360(例如360 + x)还是仅设置角度x,它都会按照您的期望进行插值,而不会尝试朝相反的方向射击。

I hope that helps! 希望对您有所帮助!

EDIT: As @JoeBlow mentioned in the comments above, I also suggest using any one of the number of Rotate methods (on both transforms and quaternions), rather than setting target values directly. 编辑:正如@JoeBlow在上面的评论中提到的,我还建议使用任何数量的Rotate方法(在转换和四元数上),而不是直接设置目标值。 In most cases, it will ensure smooth rotation, however quickly or slowly you want it. 在大多数情况下,它可以确保平稳旋转,但是您可以快速或缓慢地旋转它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM