闭路电视控制(unity3D)

时间:2017-08-11 11:42:49

标签: unity3d

如何让闭路电视控制像这样的相机 - Camera 。也就是说,它位于天花板上,它的旋转受X和Y的限制。就像这里 -

 public float Smoothness = 0.3F;
    public Vector2 Sensitivity = new Vector2(4, 4);
    public Vector2 LimitX = new Vector2(-70, 80);
    public Vector2 LimitY = new Vector2(-60, 20);

    private Vector2 NewCoord;
    public Vector2 CurrentCoord;
    private Vector2 vel;

    void Update()
    {
        NewCoord.x = Mathf.Clamp(NewCoord.x, LimitX.x, LimitX.y);
        NewCoord.y = Mathf.Clamp(NewCoord.y, LimitY.x, LimitY.y);
        NewCoord.x -= Input.GetAxis("Mouse Y") * Sensitivity.x;
        NewCoord.y += Input.GetAxis("Mouse X") * Sensitivity.y;
        CurrentCoord.x = Mathf.SmoothDamp(CurrentCoord.x, NewCoord.x, ref vel.x, Smoothness / 2);
        CurrentCoord.y = Mathf.SmoothDamp(CurrentCoord.y, NewCoord.y, ref vel.y, Smoothness / 2);
        transform.rotation = Quaternion.Euler(CurrentCoord.x, CurrentCoord.y, 0);
    }

但我的版本工作不正确。 谢谢!

1 个答案:

答案 0 :(得分:0)

检查x和y发生的情况。例如:

NewCoord.x -= Input.GetAxis("Mouse Y") * Sensitivity.x;
NewCoord.y += Input.GetAxis("Mouse X") * Sensitivity.y;

应该是:

NewCoord.x -= Input.GetAxis("Mouse X") * Sensitivity.x;
NewCoord.y += Input.GetAxis("Mouse Y") * Sensitivity.y;

究竟是什么"工作不正确" ?

更新

  

"工作不正常" - 我设置了LimitX和LimitY值,但由于旋转的限制,我无法使相机在天花板上产生影响

- >您正在限制范围,以及您操纵NewCoord之后。

问题(您的代码已评论):

// Clamping is done here:
NewCoord.x = Mathf.Clamp(NewCoord.x, LimitX.x, LimitX.y);
NewCoord.y = Mathf.Clamp(NewCoord.y, LimitY.x, LimitY.y);

// Clamped values get manipulated here, AFTER clamping,
// values will probably exceed clamped (Limited) Range
NewCoord.x -= Input.GetAxis("Mouse Y") * Sensitivity.x;
NewCoord.y += Input.GetAxis("Mouse X") * Sensitivity.y;

所以你需要做的就是翻转这两个线对:

<强>解决方案:

// Input Values are applied here
NewCoord.x -= Input.GetAxis("Mouse Y") * Sensitivity.x;
NewCoord.y += Input.GetAxis("Mouse X") * Sensitivity.y;

// Clamping is done here, to guarantee values are between chosen Limits
NewCoord.x = Mathf.Clamp(NewCoord.x, LimitX.x, LimitX.y);
NewCoord.y = Mathf.Clamp(NewCoord.y, LimitY.x, LimitY.y);