美文网首页
unity虚拟相机cinemachine 之ScriptingE

unity虚拟相机cinemachine 之ScriptingE

作者: 吉凶以情迁 | 来源:发表于2022-07-27 14:09 被阅读0次

    我从demo里面找到了脚本的源码,运行的效果,是5秒切换到这个cube立方体,又5秒切换到另外一个 cylinder,

    
    
    public class ScriptingExample : MonoBehaviour
    {
        CinemachineVirtualCamera vcam;
        CinemachineFreeLook freelook;
    
        void Start()
        {
            // Create a Cinemachine brain on the main camera
            var brain = GameObject.Find("Main Camera").AddComponent<CinemachineBrain>();
            brain.m_ShowDebugText = true;
            brain.m_DefaultBlend.m_Time = 1;
    
            // Create a virtual camera that looks at object "Cube", and set some settings
            vcam = new GameObject("VirtualCamera").AddComponent<CinemachineVirtualCamera>();
            vcam.m_LookAt = GameObject.Find("Cube").transform;
            vcam.m_Priority = 10;
            vcam.gameObject.transform.position = new Vector3(0, 1, 0);
    
            // Install a composer.  You can install whatever CinemachineComponents you need,
            // including your own custom-authored Cinemachine components.
            var composer = vcam.AddCinemachineComponent<CinemachineComposer>();
            composer.m_ScreenX = 0.30f;
            composer.m_ScreenY = 0.35f;
    
            // Create a FreeLook vcam on object "Cylinder"
            freelook = new GameObject("FreeLook").AddComponent<CinemachineFreeLook>();
            freelook.m_LookAt = GameObject.Find("Cylinder/Sphere").transform;
            freelook.m_Follow = GameObject.Find("Cylinder").transform;
            freelook.m_Priority = 11;
    
            // You can access the individual rigs in the freeLook if you want.
            // FreeLook rigs come with Composers pre-installed.
            // Note: Body MUST be Orbital Transposer.  Don't change it.
            CinemachineVirtualCamera topRig = freelook.GetRig(0);
     /*       CinemachineVirtualCamera middleRig = freelook.GetRig(1);
            CinemachineVirtualCamera bottomRig = freelook.GetRig(2);
            topRig.GetCinemachineComponent<CinemachineComposer>().m_ScreenY = 0.35f;
            middleRig.GetCinemachineComponent<CinemachineComposer>().m_ScreenY = 0.25f;
            bottomRig.GetCinemachineComponent<CinemachineComposer>().m_ScreenY = 0.15f;*/
        }
    
        float lastSwapTime = 0;
        void Update()
        {
            // Switch cameras from time to time to show blending
            if (Time.realtimeSinceStartup - lastSwapTime > 5)//每5秒递增。
            {
                    print("Time.realtimeSinceStartup " + Time.realtimeSinceStartup + ",lastSwapTime:" + lastSwapTime);
                freelook.enabled = !freelook.enabled;
                lastSwapTime = Time.realtimeSinceStartup;
            }
        }
    }
    

    Time.realtimeSinceStartup表示启动开始的累计时间, 如果大于5秒 ,

    就把某个虚拟相机禁用或者启用,因为这个优先级是11,如果启用,则镜头会移动到11这个。
    其次lastSwapTime每次打印会一次是5秒,10 秒 10秒,这个不难理解,

    image.png

    另外这个红色的立方体是一直在自转,从源码中分析发现没有写相关代码。
    其次当镜头切换到圆柱的时候是可以滚动鼠标让视图变化的。

            freelook.m_LookAt = GameObject.Find("Cylinder/Sphere").transform;
    
    

    lookat代表镜头的角度跟随 物体

    
            freelook.m_Follow = GameObject.Find("Cylinder").transform;
    

    代表镜头始终贴近物体,无论物体移动到哪。

    至于CinemachineVirtualCamera middleRig = freelook.GetRig(1)没看懂具体干嘛的

    相关文章

      网友评论

          本文标题:unity虚拟相机cinemachine 之ScriptingE

          本文链接:https://www.haomeiwen.com/subject/mydzirtx.html