2025-03-10 10:18:11 +08:00

58 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
/********************************************************************************
*Create By CG
*Function 摄像机环绕某个物体旋转展示
*********************************************************************************/
namespace ZXK.UTility
{
public class CameraAroundCtrl : MonoBehaviour
{
/// <summary>
/// 需要围绕的物体
/// </summary>
public Transform _CenObj;
//private float _disCen;
private void Update()
{
if (!EventSystem.current.IsPointerOverGameObject())
{
SetCamDistance();
SetCamRotation();
}
}
/// <summary>
/// 设置摄像机距离物体远近
/// </summary>
private void SetCamDistance()
{
if(Input.GetAxis("Mouse ScrollWheel") > 0)
{
transform.Translate(Vector3.forward * 0.5f);
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
transform.Translate(Vector3.forward * -0.5f);
}
}
/// <summary>
/// 设置摄像机围绕物体旋转
/// </summary>
private void SetCamRotation()
{
float mouse_x = Input.GetAxis("Mouse X");
float mouse_y = Input.GetAxis("Mouse Y");
if (Input.GetKey(KeyCode.Mouse1))
{
transform.RotateAround(_CenObj.position, transform.up, mouse_x * 5);
transform.RotateAround(_CenObj.position, transform.right, -mouse_y * 5);
}
}
}
}