使用Cinemachine Confiner设置摄像机边界

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

​ main camera

​ 另外一个相机

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

​ 绑定边界

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

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

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

​ bounds对象添加好标签

​ 给相机装上脚本,找到我们的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();

CinemachineConfiner confiner = GetComponent();

confiner.m_BoundingShape2D = confinerShape;

//清除缓存

confiner.InvalidatePathCache();

}

}

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

查看原文