2017-08-11 87 views
-1

如何让闭路电视控制这里的摄像头 - Camera 。也就是说,它是在天花板上,它是在旋转的X限制和Y是喜欢这里 -闭路电视控制(unity3D)

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); 
    } 

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

+0

对于连续使用Quaternion.euler(欧拉角)轮换是一种不好的做法:https://www.sjbaker.org/steve/omniv/eulers_are_evil.html。您应该使用transform.Rotate()或transform.RotateAround(),因为它们不会使用EulerAngles执行旋转。另一方面,如果你需要存储一个给定的位置,eulerAngles很好,因为它们的可读性! – Greg

+0

我发现这个脚本 'public float speedH = 2.0f; public float speedV = 2.0f; 私人浮动偏航= 0.0f; 私人浮动间距= 0.0f; void Update(){ yaw + = speedH * Input.GetAxis(“Mouse X”); pitch - = speedV * Input.GetAxis(“Mouse Y”); transform.eulerAngles = new Vector3(pitch,yaw,0.0f); ',但我无法弄清楚这个限制。如果我把 'if(gameObject.transform.rotation.x <116F) {pitch} - = speedV * Input.GetAxis(“Mouse Y”); }' 然后就不会有反应 – 50VAJJ

+0

不好意思。我无法正确格式化 – 50VAJJ

回答

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); 
+0

NewCoord.x - = Input.GetAxis(“Mouse Y”)* Sensitivity.x; NewCoord.y + = Input.GetAxis(“Mouse X”)* Sensitivity.y;否则会有逆向控制。 “工作不正确” - 我设置了LimitX和LimitY值,但是我无法在旋转限制的情况下使摄像头在天花板上产生效果 – 50VAJJ