2016-10-04 46 views
2

我的代碼不工作,我想夾住相機,但它不工作。它瞬間飆升到45。我怎樣夾緊相機?鉗RotateAround不工作的攝像頭在Unity3D

這是我的代碼。

using UnityEngine; 
using System.Collections; 

public class MoveCamera : MonoBehaviour 
{ 

    public float sensitivity = 4.0f;   
    private Vector3 mouseOrigin; 
    private bool isRotating; 

    private float minX = -45.0f; 
    private float maxX = 45.0f; 

    private float minY = -10.0f; 
    private float maxY = 10.0f; 

    float rotationY = 0.0f; 
    float rotationX = 0.0f; 

    void Start() 
    { 

    } 

    void Update() 
    { 

     if (Input.GetMouseButtonDown (0)) { 

      mouseOrigin = Input.mousePosition; 
      isRotating = true; 
     } 

     if (!Input.GetMouseButton (0)) 
      isRotating = false; 

     if (isRotating) { 

      Vector3 pos = Camera.main.ScreenToViewportPoint (Input.mousePosition - mouseOrigin); 
      transform.RotateAround (transform.position, transform.right, -pos.y * sensitivity); 
      transform.RotateAround (transform.position, Vector3.up, pos.x * sensitivity); 

      rotationY = Mathf.Clamp (transform.localEulerAngles.y, minY, maxY); 
      rotationX = Mathf.Clamp (transform.localEulerAngles.x, minX, maxX); 
      transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0); 
     } 
    } 
} 
+0

transform.RotateAround實際上會旋轉變換,所以每次更新都要旋轉,然後再使用transform.localEulerAngles旋轉。你想要相機做什麼? – Absinthe

+0

我想要相機將Y軸固定到10和-10,X軸固定到45和-45 –

+0

如果我刪除最後3行,它的旋轉完美,因爲我想要的。但我想鉗,所以我添加了最後3行,但它沒有夾緊。 –

回答

0

我固定它。這是完整的代碼。

using UnityEngine; 
using System.Collections; 

public class MoveCamera : MonoBehaviour 
{ 

    public float sensitivity = 4.0f;   
    private Vector3 mouseOrigin; 
    private bool isRotating; 
    public GameObject cam; 

    void Start() 
    { 
    } 

    protected float ClampAngle(float angle, float min, float max) { 

     angle = NormalizeAngle(angle); 
     if (angle > 180) { 
      angle -= 360; 
     } else if (angle < -180) { 
      angle += 360; 
     } 

     min = NormalizeAngle(min); 
     if (min > 180) { 
      min -= 360; 
     } else if (min < -180) { 
      min += 360; 
     } 

     max = NormalizeAngle(max); 
     if (max > 180) { 
      max -= 360; 
     } else if (max < -180) { 
      max += 360; 
     } 

     return Mathf.Clamp(angle, min, max); 
    } 

    protected float NormalizeAngle(float angle) { 
     while (angle > 360) 
      angle -= 360; 
     while (angle < 0) 
      angle += 360; 
     return angle; 
    } 


    void Update() 
    { 

     if (Input.GetMouseButtonDown (0)) { 

      mouseOrigin = Input.mousePosition; 
      isRotating = true; 
     } 

     if (!Input.GetMouseButton (0)) 
      isRotating = false; 

     if (isRotating) { 

      cam.transform.localEulerAngles = new Vector3(0, ClampAngle(cam.transform.localEulerAngles.y, -45, 45), 0); 
      Vector3 pos = Camera.main.ScreenToViewportPoint (Input.mousePosition - mouseOrigin); 
      transform.RotateAround (transform.position, transform.right, -pos.y * sensitivity); 
      transform.RotateAround (transform.position, Vector3.up, pos.x * sensitivity); 
     } 
    } 
} 
0

下面是一個限制Y軸旋轉的示例,您可以將它適用於X.你沒有說明限制應該基於什麼,所以這裏是基於另一個對象(公共變換目標)的旋轉,這可能是你的玩家或其他任何東西。

public float sensitivity = 16.0f; 
public Transform target; 

void Update() 
{ 

    if (Input.GetMouseButton(0)) 
    { 
     //Debug.Log(Quaternion.Angle(transform.rotation, target.rotation)); 
     float angle = Quaternion.Angle(transform.rotation, target.rotation); 
     if(angle < 45 || angle > 315) 
     { 
      Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition); 
      transform.RotateAround(pos, Vector3.up, Time.deltaTime * sensitivity); 
     } 

    } 
} 

相反,如果你想限制基於世界空間只是檢查相機的旋轉:在這兩種情況下使用Time.deltaTime,保證旋轉出現在發生

public float sensitivity = 16.0f; 
//public Transform target; 

void Update() 
{ 

    if (Input.GetMouseButton(0)) 
    { 

     float angle = transform.eulerAngles.y; 
     Debug.Log(angle); 
     if (angle < 45 || angle > 315) 
     { 
      Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition); 
      transform.RotateAround(pos, Vector3.up, Time.deltaTime * sensitivity); 
     } 

    } 
} 

注無論玩家的幀率如何,速度都一樣。

如果你想扭轉旋轉反轉RotateAround軸參數:

transform.RotateAround(pos, -Vector3.up, Time.deltaTime * sensitivity);