Unity--使用Cinemachine Confiner设置摄像机边界

使用Cinemachine Confiner设置摄像机边界

前提提要:在做这个功能前需要:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lNvWCB52-1685791858456)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230603190732848.png)]

​ main camera

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FVmCuqWJ-1685791858457)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230603190818928.png)]

​ 另外一个相机

思路:创建一个对象绑定Polygon Collider2D 边界。然后在另外一个相机Cinemachine Confiner上绑定他

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UKetMX8d-1685791858457)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230603191213933.png)]

​ 绑定边界

记得点这个,否则会收重力影响

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8UMfTbRr-1685791858457)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230603191607060.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-arNUPjhx-1685791858458)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230603191432472.png)]

​ 在另外一个相机Cinemachine Confiner上绑定他

我们用代码尝试找到我们的bouns对象

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ivs2L6Hi-1685791858458)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230603192440299.png)]

​ bounds对象添加好标签

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TNBUyOxA-1685791858459)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230603192515965.png)]

​ 给相机装上脚本,找到我们的bounds对象

SwitchBounds.cs

using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using UnityEngine;

public class SwitchBounds : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        SwitchConfinerShape();
    }

   private void SwitchConfinerShape()
    {
    
    
        PolygonCollider2D confinerShape = GameObject.FindGameObjectWithTag("BoundsConfiner").GetComponent<PolygonCollider2D>();

        CinemachineConfiner confiner = GetComponent<CinemachineConfiner>();

        confiner.m_BoundingShape2D = confinerShape;

        //清除缓存
        confiner.InvalidatePathCache();
    }
}

就完成了我们的移动,这样我们的角色就走不出地图。

猜你喜欢

转载自blog.csdn.net/abaidaye/article/details/131024762
今日推荐