使用Cinemachine Confiner设置摄像机边界
前提提要:在做这个功能前需要:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lNvWCB52-1685791858456)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230603190732848.png)]](https://img-blog.csdnimg.cn/38de2b6ff4744fe78f4d2ac70421846c.png)
main camera
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FVmCuqWJ-1685791858457)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230603190818928.png)]](https://img-blog.csdnimg.cn/d5a92cf5a15344219001d0f7826a1ab2.png)
另外一个相机
思路:创建一个对象绑定Polygon Collider2D 边界。然后在另外一个相机Cinemachine Confiner上绑定他
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UKetMX8d-1685791858457)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230603191213933.png)]](https://img-blog.csdnimg.cn/2d355d6518f848159d313fe5a75fe6f3.png)
绑定边界
记得点这个,否则会收重力影响
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8UMfTbRr-1685791858457)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230603191607060.png)]](https://img-blog.csdnimg.cn/5eb932297b7f4d58a622a9b4dbacfaa9.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-arNUPjhx-1685791858458)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230603191432472.png)]](https://img-blog.csdnimg.cn/7e69d6b0bb0f4073b274463a8f413682.png)
在另外一个相机Cinemachine Confiner上绑定他
我们用代码尝试找到我们的bouns对象
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ivs2L6Hi-1685791858458)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230603192440299.png)]](https://img-blog.csdnimg.cn/bd524a8f89ee491da35a1c43ce9dd007.png)
bounds对象添加好标签
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TNBUyOxA-1685791858459)(C:/Users/86188/AppData/Roaming/Typora/typora-user-images/image-20230603192515965.png)]](https://img-blog.csdnimg.cn/d529cc971c0747799dc9c853df22636f.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();
}
}
就完成了我们的移动,这样我们的角色就走不出地图。



















