Cinemachine相机间的切换

在Cinemachine中,如果两个相机的priority值是一样的,一个相机启用,另外一个相机是禁用的,如果把禁用的相机再启用,Cinemachine Brain的Live Camera会切换到这个相机,这样就可以实现相机的切换。可以通过触发器代码来切换相机。

首先在空物体是上挂一个脚本,控制首先Live Camera选择的是哪个相机。

ActiveCamOnPlay.cs

using UnityEngine;

namespace Cinemachine.Examples
{

[AddComponentMenu("")] // Don't display in add component menu
public class ActivateCamOnPlay : MonoBehaviour
{
    public CinemachineVirtualCameraBase vcam;

	// Use this for initialization
	void Start () 
    {
	    if (vcam)
	    {
	        vcam.MoveToTopOfPrioritySubqueue();
	    }
	}
}

}
Cinemachine相机间的切换_第1张图片,如图,把首先要启用的虚拟相机赋值给Vcam

切换的Trriger脚本。

using UnityEngine;

namespace Cinemachine.Examples
{

[AddComponentMenu("")] // Don't display in add component menu
public class ActivateObject : MonoBehaviour
{
    public GameObject cartToMove;//PathObject,这是一个Dolly Track
    public CinemachineVirtualCameraBase switchToCam; //Camera
    public CinemachinePathBase.PositionUnits positionUnits = CinemachinePathBase.PositionUnits.Distance;
    public float speed = 5f;

    private CinemachineDollyCart dCartComp;
    void Start()
    {
        if (cartToMove)
        {
            dCartComp = cartToMove.GetComponent();//获取CinemachineDollyCart,并令其速度为0,并不运行
            dCartComp.m_Speed = 0f;    
        }
        
    }

    void OnTriggerEnter(Collider col)
    {
        if (col.CompareTag("Player") && switchToCam && cartToMove)
        {
            if (Camera.main.GetComponent().ActiveVirtualCamera.Name != switchToCam.Name)
            {
                dCartComp.m_PositionUnits = positionUnits;
                dCartComp.m_Speed = speed;

                switchToCam.VirtualCameraGameObject.SetActive(false); //先禁用再启用,得到切换效果
                switchToCam.VirtualCameraGameObject.SetActive(true);        
            }
        }
    }

    void OnTriggerExit(Collider col)
    {
        if (col.CompareTag("Player") && switchToCam != null && cartToMove)
        {
            dCartComp.m_Speed = 0f;
            switchToCam.VirtualCameraGameObject.SetActive(false);
        }
    }
}

}

你可能感兴趣的:(Cinemachine相机间的切换)