相机围绕物体旋转代码,可自行复制粘贴使用。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//摄像机操作
//删减版 在实际的使用中可能会有限制的需求 比如最大远离多少 最近距离多少 不能旋转到地面以下等
public class RotateAround : MonoBehaviour
{
public Transform CenObj; //围绕的物体
public RawImage RawImage; //ui矩形范围
private Vector3 Rotion_Transform; //物体的位置,围绕这个位置旋转
public Vector2 UILeftDownScreenPosition; //UI的左下角坐标
public Vector2 UIRightUptScreenPosition; //UI的右上角坐标
private Vector2 UICenterPointPosition; //UI的中心点位置
private float UIWight; //UI宽
private float UIHeight; //UI高
public bool IslimitedRsange; //是否限定鼠标范围
public bool IsPC; //是否是电脑,电脑就用mouse 某则就用touch
void Start()
{
#if UNITY_ANDROID
IsPC = false;
#endif
#if UNITY_EDITOR
IsPC = true ;
#endif
Rotion_Transform = CenObj.position;
UIWight = RawImage.GetComponent<RawImage>().rectTransform.rect.width;
UIHeight = RawImage.GetComponent<RawImage>().rectTransform.rect.height;
UICenterPointPosition.x = RawImage.GetComponent<RawImage>().transform.position.x;
UICenterPointPosition.y = RawImage.GetComponent<RawImage>().transform.position.y;
UILeftDownScreenPosition = new Vector2(UICenterPointPosition.x - UIWight / 2, UICenterPointPosition.y - UIHeight / 2);
// Debug.Log("qian" + UIRightUptScreenPosition.y);
UIRightUptScreenPosition = new Vector2(UICenterPointPosition.x + UIWight / 2, UICenterPointPosition.y + UIHeight / 2);
// Debug.Log("qian" + UIRightUptScreenPosition.y);
}
void Update()
{
// Ctrl_Cam_Move(); //远近
Cam_Ctrl_Rotation(); //旋转
//if (Input.GetMouseButtonDown(0))
//{
// start = Input.mousePosition;
//}
//if (Input.GetMouseButton(0))
// MyRoat();
}
/// <summary>
/// 镜头的远离和接近
/// </summary>
public void Ctrl_Cam_Move()
{
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
transform.Translate(Vector3.forward * 1f);//速度可调 自行调整
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
transform.Translate(Vector3.forward * -1f);//速度可调 自行调整
}
}
/// <summary>
/// 谁便测的
/// </summary>
public Vector3 start;
public Vector3 dis;
public Vector3 disa;
public Vector3 over;
public void MyRoat()
{
over = Input.mousePosition;
dis = over - start;
disa.x = -dis.y;
disa.y = dis.x;
transform.RotateAround(Rotion_Transform, disa, 2.6f);
//if (Mathf.Abs(disa.x) > Mathf.Abs(disa.y))
//{
// //x轴移动
// transform.RotateAround(Rotion_Transform, CenObj.right, disa.y); //上下移动
//}
//else
//{
// //y轴移动
// transform.RotateAround(Rotion_Transform, CenObj.up, disa.x);
//}
//预设角度(当前角度加上将要增加/减少的角度)
float rotatedAngle = transform.eulerAngles.x + disa.y+1;
旋转角度小于15则设置为15
if (rotatedAngle < 15)
{
// disa .y= disa.y * 2.6f;
transform.RotateAround(Rotion_Transform, Vector3.up, disa.x); //上下移动
}
else if (rotatedAngle > 85) //旋转角度大于85则设置85
{
// disa.y = disa.y * 2.6f;
transform.RotateAround(Rotion_Transform, Vector3.up, disa.x); //上下移动
}
else
{
transform.RotateAround(Rotion_Transform, transform.right, disa.y );
}
//transform.RotateAround(Rotion_Transform, Vector3.up, disa.x );
start = over;
}
/// <summary>
/// 摄像机的旋转
/// </summary>
public void Cam_Ctrl_Rotation()
{
//利用两点之间的 向量插值计算
// Debug.Log("鼠标" +Input.mousePosition);
var mouse_x = Input.GetAxis("Mouse X");//获取鼠标X轴移动
var mouse_y = -Input.GetAxis("Mouse Y");//获取鼠标Y轴移动
//如果是电脑那么就执行这个
if (IsPC == true){
//管理器是否允许旋转这个物体
if (AssmbleManager.Instance.IsScreenRotate)
//电脑中按下鼠标左键
if (Input.GetMouseButton(0))
{
//限制角度
if (IslimitedRsange)
{
//检测区域 ,自已自己设置
if (Input.mousePosition.x > UILeftDownScreenPosition.x && Input.mousePosition.y > UILeftDownScreenPosition.y)
{
if (Input.mousePosition.x < UIRightUptScreenPosition.x && Input.mousePosition.y < UIRightUptScreenPosition.y)
{
//预设角度(当前角度加上将要增加/减少的角度)
float rotatedAngle = transform.eulerAngles.x + mouse_y * 8;
//旋转角度小于15则设置为15
if (rotatedAngle < 15)
{
transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 8 + (15 - rotatedAngle));
}
else if (rotatedAngle > 85) //旋转角度大于85则设置85
{
//transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 5 - rotatedAngle);
}
else
{
transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 8);
}
transform.RotateAround(Rotion_Transform, Vector3.up, mouse_x * 8);
}
}
}
else
{
//检测区域
if (Input.mousePosition.y > UILeftDownScreenPosition.y)
{
//预设角度(当前角度加上将要增加/减少的角度)
float rotatedAngle = transform.eulerAngles.x + mouse_y * 8;
//旋转角度小于15则设置为15
if (rotatedAngle < 15)
{
transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 8 + (15 - rotatedAngle));
}
else if (rotatedAngle > 85) //旋转角度大于85则设置85
{
//transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 5 - rotatedAngle);
}
else
{
transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 8);
}
transform.RotateAround(Rotion_Transform, Vector3.up, mouse_x * 8);
}
}
}
}
else
{
//在手机上
if (AssmbleManager.Instance.IsScreenRotate)
//如果在安卓手机上
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
Debug.LogError("sdas");
//限制角度
if (IslimitedRsange)
{
//检测区域
if (Input.mousePosition.x > UILeftDownScreenPosition.x && Input.mousePosition.y > UILeftDownScreenPosition.y+100)
{
if (Input.mousePosition.x < UIRightUptScreenPosition.x && Input.mousePosition.y < UIRightUptScreenPosition.y)
{
//预设角度(当前角度加上将要增加/减少的角度)
float rotatedAngle = transform.eulerAngles.x + mouse_y * 8;
//旋转角度小于15则设置为15
if (rotatedAngle < 15)
{
transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 8 + (15 - rotatedAngle));
}
else if (rotatedAngle > 85) //旋转角度大于85则设置85
{
//transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 5 - rotatedAngle);
}
else
{
transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 8);
}
transform.RotateAround(Rotion_Transform, Vector3.up, mouse_x * 8);
}
}
}
else
{
//检测区域
if (Input.mousePosition.y > UILeftDownScreenPosition.y)
{
//预设角度(当前角度加上将要增加/减少的角度)
float rotatedAngle = transform.eulerAngles.x + mouse_y * 8;
//旋转角度小于15则设置为15
if (rotatedAngle < 15)
{
transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 8 + (15 - rotatedAngle));
}
else if (rotatedAngle > 85) //旋转角度大于85则设置85
{
//transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 5 - rotatedAngle);
}
else
{
transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 8);
}
transform.RotateAround(Rotion_Transform, Vector3.up, mouse_x * 8);
}
}
}
}
}
}
}
角度限制:思路就是提前计算会不会超过
//预设角度(当前角度加上将要增加/减少的角度)
float rotatedAngle = transform.eulerAngles.x + mouse_y * 8;
//旋转角度小于15则设置为15
if (rotatedAngle < 15)
{
transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 8 + (15 - rotatedAngle));
}
else if (rotatedAngle > 85) //旋转角度大于85则设置85
{
//transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 5 - rotatedAngle);
}
else
{
transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 8);
}
transform.RotateAround(Rotion_Transform, Vector3.up, mouse_x * 8);
解决安卓手机点击位置出现的问题,getmouse()在手机上第一次按下会出现一个位置。不要使用mouse鼠标点击
//如果在手机上使用这个条件判断,不要用 getmouse()等鼠标判断,点击第一下会出现问题。
if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)