91 lines
2.8 KiB
C#
91 lines
2.8 KiB
C#
using Cinemachine;
|
|
using EasyInject.Attributes;
|
|
using UnityEngine;
|
|
/// <summary>
|
|
/// 相机的跟随
|
|
/// </summary>
|
|
[GameObjectBean]
|
|
public class CameraFollow : MonoBehaviour
|
|
{
|
|
[Header("上下偏移")]
|
|
public float y;
|
|
[Header("左右偏移")]
|
|
public float z;
|
|
public float speed = 120;
|
|
[Autowired]
|
|
private CameraManage cameraManage;
|
|
public CinemachineVirtualCamera virtualCamera;
|
|
/// <summary>
|
|
/// 主要控制旋转的句柄
|
|
/// </summary>
|
|
private GameObject target;
|
|
private void Update()
|
|
{
|
|
mouseRoundRoller();
|
|
}
|
|
void mouseRoundRoller()
|
|
{
|
|
if(target == null)
|
|
{
|
|
return;
|
|
}
|
|
//不得当前活动相机返回
|
|
if (!CinemachineCore.Instance.IsLive(virtualCamera))
|
|
{
|
|
return;
|
|
}
|
|
if (Input.GetMouseButton(0)) // 检查鼠标左键是否按下
|
|
{
|
|
// 根据鼠标移动量调整旋转角度
|
|
float mouseX = Input.GetAxis("Mouse X") * speed * Time.deltaTime;
|
|
float y = target.transform.rotation.eulerAngles.y;
|
|
y -= mouseX;
|
|
// 应用新的旋转角度
|
|
target.transform.rotation = Quaternion.Euler(0, y, 0);
|
|
//transform.position = transform.position - transform.forward;
|
|
}
|
|
|
|
}
|
|
public void OnEnter(CinemachineVirtualCamera cinemachineCamera)
|
|
{
|
|
cinemachineCamera.LookAt = transform;
|
|
cinemachineCamera.Follow = transform;
|
|
CinemachineTransposer transposer = cinemachineCamera.GetCinemachineComponent<CinemachineTransposer>();
|
|
if (transposer != null )
|
|
{
|
|
cameraManage.IsPlaying = false;
|
|
transposer.m_FollowOffset.y = y;
|
|
transposer.m_FollowOffset.z = z;
|
|
ICinemachineCamera cinemachine = cameraManage.GetMaxPriorityCamera();
|
|
cinemachineCamera.Priority = cinemachine.Priority +1;
|
|
cinemachine.Priority = 0;
|
|
|
|
}
|
|
}
|
|
public void OnEnter()
|
|
{
|
|
if (target == null)
|
|
{
|
|
target = new GameObject(name + "_camera");
|
|
target.transform.parent = transform;
|
|
target.transform.localPosition = Vector3.zero;
|
|
}
|
|
virtualCamera.LookAt = target.transform;
|
|
virtualCamera.Follow = target.transform;
|
|
CinemachineTransposer transposer = virtualCamera.GetCinemachineComponent<CinemachineTransposer>();
|
|
if (transposer != null)
|
|
{
|
|
cameraManage.IsPlaying = false;
|
|
//cameraManage.playable.Stop();
|
|
transposer.m_FollowOffset.y = y;
|
|
transposer.m_FollowOffset.z = z;
|
|
ICinemachineCamera cinemachine = cameraManage.GetMaxPriorityCamera();
|
|
int back = cinemachine.Priority;
|
|
Debug.LogWarning("相机切换="+transform.name);
|
|
virtualCamera.Priority = cinemachine.Priority + 1;
|
|
cinemachine.Priority = 0;
|
|
}
|
|
}
|
|
|
|
}
|