一.旋转相机
private float rotateSpeed; // 旋转速度
private bool isPlatform;
private Quaternion cameraInitRota;//相机初始角度
private Vector3 cameraInitPos;//相机初始位置
public Transform target;//人物父物体
public Transform cameraTransform; // 相机对象
public bool isRota;
private void Awake()
{
cameraInitRota = cameraTransform.localRotation;
cameraInitPos = cameraTransform.localPosition;
JudgPlatform();
}
private void Update()
{
RotaRole();
}
/// <summary>
/// 重置视角
/// </summary>
public void ResetFar()
{
cameraTransform.localRotation = cameraInitRota;
cameraTransform.localPosition = cameraInitPos;
}
/// <summary>
/// 判断当前平台设置对应旋转速度
/// </summary>
private void JudgPlatform()
{
isRota = false;
if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
isPlatform = true;
rotateSpeed = 0.2f;
}
else
{
rotateSpeed = 5f;
}
}
/// <summary>
/// 旋转模型
/// </summary>
private void RotaRole()
{
if (!isRota) return;
if (isPlatform)
{
// 处理触摸输入
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
// 在屏幕上滑动手指,旋转相机
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
float rotationX = touchDeltaPosition.x * rotateSpeed;
float rotationY = -touchDeltaPosition.y * rotateSpeed;
cameraTransform.transform.RotateAround(target.transform.position, Vector3.up, rotationX);
cameraTransform.transform.RotateAround(target.transform.position, cameraTransform.transform.right, rotationY);
}
}
else
{
// 处理鼠标输入
if (Input.GetMouseButton(1))
{
// 在屏幕上滑动鼠标,旋转相机
float rotationX = Input.GetAxis("Mouse X") * rotateSpeed;
float rotationY = -Input.GetAxis("Mouse Y") * rotateSpeed;
cameraTransform.transform.RotateAround(target.transform.position, Vector3.up, rotationX);
cameraTransform.transform.RotateAround(target.transform.position, cameraTransform.transform.right, rotationY);
}
}
}
二.旋转模型
private float rotateSpeed; // 旋转速度
private bool isPlatform;//判断平台
private Quaternion modelInitRota;//模型初始角度
private Vector3 modelInitPos;//模型初始位置
private float lastMouseX; // 上一帧鼠标的 X 坐标
public bool isRota;//指定页面旋转值
public Transform target;//人物父物体
private void Awake()
{
rolePath = Application.persistentDataPath + "/File/";
modelInitRota = target.localRotation;
modelInitPos = target.localPosition;
JudgPlatform();
}
private void Update()
{
RotaRole();
}
/// <summary>
/// 重置视角
/// </summary>
public void ResetFar()
{
target.localRotation = modelInitRota;
target.localPosition = modelInitPos;
}
/// <summary>
/// 判断当前平台设置对应旋转速度
/// </summary>
private void JudgPlatform()
{
isRota = false;
if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
isPlatform = true;
rotateSpeed = 0.2f;
}
else
{
isPlatform = false;
rotateSpeed = 1f;
}
}
/// <summary>
/// 旋转模型
/// </summary>
private void RotaRole()
{
if (!isRota) return;
if (isPlatform)
{
if (Input.touchCount == 1)
{
// 获取触摸的移动量
Touch touch = Input.GetTouch(0);
float deltaX = touch.deltaPosition.x;
// 计算旋转角度
float rotation = deltaX * rotateSpeed;
// 应用旋转
target.Rotate(0, rotation, 0);
}
}
else
{
if (Input.GetMouseButton(1)) // 鼠标右键
{
// 获取鼠标的移动量
float deltaX = Input.mousePosition.x - lastMouseX;
lastMouseX = Input.mousePosition.x;
// 计算旋转角度
float rotation = deltaX * rotateSpeed;
// 应用旋转
target.Rotate(0, rotation, 0);
}
}
}
两种方式按需求去用,摄像机旋转适合模型有动画的清空下使用