2016-10-03 3 views
0

코드가 작동하지 않습니다. 카메라를 클램프하려고하지만 작동하지 않습니다. 카메라를 고정하는 방법?Unity3D에서 카메라를 클램핑하는 방법

using UnityEngine; 
using System.Collections; 

public class MoveCamera : MonoBehaviour 
{ 

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

    private float speed = 2.0f; 

    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 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 (rotationY, minY, maxY); 
       rotationX = Mathf.Clamp (rotationX, minX, maxX); 
       transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0); 
      } 
    } 
} 

답변

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

여기

public static float Clamp(float value, float min, float max) { 
    if (value < min) { 
     value = min; 
    } else if (value > max) { 
     value = max; 
    } 
    return value; 
}

Mathf.Clamp의 코드의 사용 IL 역 도구 등 (외부 링크) ILSPY 호출 (유니티/모노/등) .NET에서 어떻게 작동하는지 확실하지 않은 경우.
게시 한 코드에 따라 Mathf.Clamp을 이해하면 의도 한대로 작동해야합니다. 코드는 적어도 한 번, 예를 들어 여기 :


rotationY = Mathf.Clamp (rotationX, minY, maxY); //note it's rotation "X" instead of "Y" 
rotationX = Mathf.Clamp (rotationX, minX, maxX);

여전히 문제를 분류하지 못하면 Debug.Log을 사용하여 실수 한 위치를 찾는 변수 값을 확인하십시오.
이렇게 분류 할 수 없다면 정확히 할 수없는 것을 명확하게 알 수있을 것이며 더 깨끗한 질문을 게시하고 깨끗한 대답을 기대할 수 있습니다.

희망이 도움이됩니다.

+0

그런데 외부 도구를 사용하고 싶지 않습니다. 그런데 "X"대신 "Y"라는 코드를 수정했습니다. 빠른 답장을 보내 주셔서 감사합니다. :) –

+0

제가 언급 한 외부 도구는 실제로 외부입니다. Unity에 전혀 영향을 미치지 않습니다 ... 그리고 제가 게시 한 픽스가 아직 충분하지 않은 경우 코드에서 Debug.Log를 사용하여 원하는 것 대신 실제로 무엇을하는지보십시오. 마찬가지로, Mathf.Clamp는 의도 한대로 작동합니다. 심지어 코드에서도 ... –

2

회전 후 회전에서 rotationX 및 rotationY 값을 가져 오는 것을 잊었습니다. 이것을 시도하십시오 :

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

작동하지 않습니다. 빠른 답장을 보내 주셔서 감사합니다. –

+0

"작동하지 않는다"고 말하면 ** 무엇이 작동하지 않는지를 설명하십시오 **) – Hellium

+0

한 위치에 딱 맞아 붙어 있습니다. 단일 지점을 회전시키지 않습니다. –

관련 문제