限制垂直相机旋转

时间:2017-12-30 04:52:02

标签: c# unity3d

我已经看过关于这个主题的多个线程,尽管这些解决方案都没有适用于我当前的脚本。我设置了相机,以便在按住并拖动鼠标右键时旋转。我的相机随着WASD键移动。

    if(Input.GetMouseButtonDown(1))
    {
        // Get mouse origin
        mouseOrigin = Input.mousePosition;
        isRotating = true;
    }

    if (isRotating)
    {
        Vector3 pos = cameraMain.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);

        transform.RotateAround(transform.position, transform.right, -pos.y * turnSpeed);
        transform.RotateAround(transform.position, Vector3.up, pos.x * turnSpeed);
    }

我对此的错误是相机可以自由垂直旋转。我想知道如何在不改变此代码对相机的影响的情况下对此旋转应用限制。

2 个答案:

答案 0 :(得分:0)

if (isRotating)
{
    Vector3 pos = cameraMain.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);

     pos.x =  Mathf.Clamp (pos.x, 0, 90);
     pos.y =  Mathf.Clamp (pos.y, 0, 90);

    transform.RotateAround("pass center object position", transform.right, -pos.y * turnSpeed);
    transform.RotateAround("pass center object position", Vector3.up, pos.x * turnSpeed);
}

答案 1 :(得分:0)

我重做了我的相机移动代码。我有这个运行所以只有在按住鼠标右键时才会调用此函数。

/* To accommodate text entry in documents that contain nested elements, or in which supplying and
 * evaluating characters at indices is an expensive proposition, a position within a text input
 * document is represented as an object, not an integer.  UITextRange and UITextPosition are abstract
 * classes provided to be subclassed when adopting UITextInput */

@available(iOS 3.2, *)
open class UITextRange : NSObject {

    open var isEmpty: Bool { get } //  Whether the range is zero-length.    

    open var start: UITextPosition { get }

    open var end: UITextPosition { get }
}