鼠标拖动屏幕控制相机旋转(含移动端)
程序员文章站
2024-03-16 20:20:52
...
public int right=75;//旋转相机y轴的最大值 (右边界)
public int left=285;//旋转相机y轴的最小值 (左边界)
public int speed=5;//相机旋转速度
PC
if(Input.GetMouseButton(0)&& !EventSystem.current.IsPointerOverGameObject()){
transform.localEulerAngles += new Vector3(0, -Input.GetAxis("Mouse X"), 0) * Time.deltaTime * speed;
//限制旋转范围
if (transform.localEulerAngles.y > right && transform.localEulerAngles.y < 180)
{
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, right, transform.localEulerAngles.z);
}
if (transform.localEulerAngles.y < left && transform.localEulerAngles.y > 180)
{
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, left, transform.localEulerAngles.z);
}
}
//使相机在-75到75之间旋转
//因为localEulerAngles.y的值是0-360,不存在负值,所以left为360-75=285。
移动端
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved&&!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
{
Vector2 touchPosition = Input.GetTouch(0).deltaPosition;
transform.localEulerAngles += new Vector3(0, -touchPosition.x, 0) * Time.deltaTime * speed;
if (transform.localEulerAngles.y > right&& transform.localEulerAngles.y < 180)
{
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, right, transform.localEulerAngles.z);
}
if (transform.localEulerAngles.y < left&& transform.localEulerAngles.y > 180)
{
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, left, transform.localEulerAngles.z);
}
}
上一篇: 求任意多边形面积
下一篇: 计算机图形学笔记 || 建模与消隐