2017-08-11 85 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