[英]How to clamp camera in Unity3D
My code is not working, I am trying to clamp the camera, but its not working. 我的代码无法正常工作,我正在尝试固定相机,但无法正常工作。 How to clamp the camera? 如何夹紧相机?
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);
}
}
}
You forgot to get the value of rotationX and rotationY from your transform after rotating it. 旋转后,您忘记从变换中获取rotationX和rotationY的值。 Try this : 尝试这个 :
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);
}
Here's the code of Mathf.Clamp
这是Mathf.Clamp
的代码
public static float Clamp(float value, float min, float max) {
if (value < min) {
value = min;
} else if (value > max) {
value = max;
}
return value;
}
Use an IL reverse tool such as (EXTERNAL LINK) ILSPY if you are unsure how a call works in .NET (Unity / Mono / etc). 如果您不确定.NET(Unity / Mono / etc)中的调用方式,请使用IL反向工具,例如(EXTERNAL LINK) ILSPY 。
Based on the code you posted and understanding Mathf.Clamp
should work as intended, the issue is most probably laying in your code, at least at one point, eg here: 根据您发布的代码并理解Mathf.Clamp
应该可以按预期工作,问题很可能至少在某一时刻出现在您的代码中,例如:
rotationY = Mathf.Clamp (rotationX, minY, maxY); //note it's rotation "X" instead of "Y"
rotationX = Mathf.Clamp (rotationX, minX, maxX);
If this still did not sort out the issue, use Debug.Log
to see variable values to find where you make the mistake(s). 如果仍然不能解决问题,请使用Debug.Log
查看变量值以查找错误的Debug.Log
。
If you can't sort out like this, you'll have a clear picture what exactly you can't do and can post a much cleaner question and expect a clean answer. 如果您无法像这样进行分类,那么您将清楚地知道无法执行的操作,并且可以发布更清晰的问题并期望得到清晰的答案。
Hope this helps! 希望这可以帮助!
OK, So I fixed it. 好,所以我修好了。 Here is complete code. 这是完整的代码。
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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.