我的代码无法正常工作,我正试图夹住相机,但它无法正常工作。它瞬间攫取到45。我该如何夹紧相机?
这是我的代码。
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 :(得分: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);
}
}
}
如果您想根据世界空间进行限制,只需检查相机的旋转:
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);
}
}
}
注意在这两种情况下都使用Time.deltaTime来确保旋转看起来以相同的速度发生,而不管玩家的帧速率如何。
如果要反转旋转,请反转RotateAround的轴参数:
transform.RotateAround(pos, -Vector3.up, Time.deltaTime * sensitivity);
答案 1 :(得分: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);
}
}
}