将相机绑定到地图
Bounding the camera to the map
我有一个 unity 的 2D 游戏,我有一个跟随我的主角的相机。
目前,例如,当我将角色向左移动时,我可以看到地图之外的内容,如下所示:
假设地图大小为 15X15。我有一个生成 15X15 大小为 1X1 的块的脚本,这意味着地图边界是 (-1, -1) -> (15, 15).
我想完成两件事:
1) 相机不要超出地图范围
2) 当地图尺寸小于相机时,让相机改变它的尺寸。
第 2 点的示例
地图是5列15行,所以跟相机比起来很窄,像这样:
甚至 10 列和 3 行,像这样:
我希望改变相机的大小,使其既不比地图宽也不高。
这两件事我该怎么做?
This is the github to the current scripts in the game。 CameraController
脚本是应该添加的地方
正交尺寸定义了垂直尺寸的一半,水平尺寸基于纵横比。
从那里您可以定义居中时的最大值。
下面的脚本应该可以让你开始。只要你的关卡是矩形的(显然包括正方形),它就没问题:
public float speed = 2f;
public Transform min;
public Transform max;
private float aspect;
private float maxSize;
private void Start ()
{
this.aspect = Camera.main.aspect;
this.maxSize = max.position.x <= max.position.y ? max.position.x /2f / this.aspect :max.position.y / 2f;
}
private void Update ()
{
float size = Input.GetAxis ("Mouse ScrollWheel");
Camera.main.orthographicSize += size;
if (Camera.main.orthographicSize > maxSize)
{
Camera.main.orthographicSize = maxSize;
}
float x = Input.GetAxis ("Horizontal");
float y = Input.GetAxis ("Vertical");
Vector3 position = this.transform.position;
position.x += x * Time.deltaTime * this.speed;
position.y += y * Time.deltaTime * this.speed;
float orthSize = Camera.main.orthographicSize;
if (position.x < (this.min.position.x + orthSize * this.aspect))
{
position.x = this.min.position.x + orthSize * this.aspect;
}
else if (position.x > (this.max.position.x - orthSize * this.aspect))
{
position.x = this.max.position.x - orthSize * this.aspect;
}
if (position.y < (this.min.position.y + orthSize))
{
position.y = this.min.position.y + orthSize;
}
else if(position.y > (this.max.position.y - orthSize))
{
position.y = this.max.position.y - orthSize;
}
this.transform.position = position;
}
想法是在左下角和右上角放置两个空的游戏对象。左下角被拖到最小值,右上角被拖到最大值。速度变量就是相机平移的速度。这是附加到相机对象。相机的最小尺寸没有限制,但你可以做到。
重点是您可以将其用于您自己的项目,因为它更通用。
剩下的只是考虑相机位置、当前大小和宽高比(您不能在运行时更改它,否则您必须修改代码)。
编辑:
如果您希望将相机变焦绑定到运动,请删除关于大小的第一行并在获得 horizontal/vertical 运动后添加以下内容:
if (x != 0f || y != 0f) {
Camera.main.orthographicSize = Mathf.MoveTowards (Camera.main.orthographicSize, largeSize, Time.deltaTime);
} else {
Camera.main.orthographicSize = Mathf.MoveTowards (Camera.main.orthographicSize, size, Time.deltaTime);
}
考虑声明 size、largeSize 变量并根据需要进行设置。
我有一个 unity 的 2D 游戏,我有一个跟随我的主角的相机。
目前,例如,当我将角色向左移动时,我可以看到地图之外的内容,如下所示:
假设地图大小为 15X15。我有一个生成 15X15 大小为 1X1 的块的脚本,这意味着地图边界是 (-1, -1) -> (15, 15).
我想完成两件事:
1) 相机不要超出地图范围
2) 当地图尺寸小于相机时,让相机改变它的尺寸。
第 2 点的示例
地图是5列15行,所以跟相机比起来很窄,像这样:
甚至 10 列和 3 行,像这样:
我希望改变相机的大小,使其既不比地图宽也不高。
这两件事我该怎么做?
This is the github to the current scripts in the game。 CameraController
脚本是应该添加的地方
正交尺寸定义了垂直尺寸的一半,水平尺寸基于纵横比。
从那里您可以定义居中时的最大值。
下面的脚本应该可以让你开始。只要你的关卡是矩形的(显然包括正方形),它就没问题:
public float speed = 2f;
public Transform min;
public Transform max;
private float aspect;
private float maxSize;
private void Start ()
{
this.aspect = Camera.main.aspect;
this.maxSize = max.position.x <= max.position.y ? max.position.x /2f / this.aspect :max.position.y / 2f;
}
private void Update ()
{
float size = Input.GetAxis ("Mouse ScrollWheel");
Camera.main.orthographicSize += size;
if (Camera.main.orthographicSize > maxSize)
{
Camera.main.orthographicSize = maxSize;
}
float x = Input.GetAxis ("Horizontal");
float y = Input.GetAxis ("Vertical");
Vector3 position = this.transform.position;
position.x += x * Time.deltaTime * this.speed;
position.y += y * Time.deltaTime * this.speed;
float orthSize = Camera.main.orthographicSize;
if (position.x < (this.min.position.x + orthSize * this.aspect))
{
position.x = this.min.position.x + orthSize * this.aspect;
}
else if (position.x > (this.max.position.x - orthSize * this.aspect))
{
position.x = this.max.position.x - orthSize * this.aspect;
}
if (position.y < (this.min.position.y + orthSize))
{
position.y = this.min.position.y + orthSize;
}
else if(position.y > (this.max.position.y - orthSize))
{
position.y = this.max.position.y - orthSize;
}
this.transform.position = position;
}
想法是在左下角和右上角放置两个空的游戏对象。左下角被拖到最小值,右上角被拖到最大值。速度变量就是相机平移的速度。这是附加到相机对象。相机的最小尺寸没有限制,但你可以做到。
重点是您可以将其用于您自己的项目,因为它更通用。
剩下的只是考虑相机位置、当前大小和宽高比(您不能在运行时更改它,否则您必须修改代码)。
编辑: 如果您希望将相机变焦绑定到运动,请删除关于大小的第一行并在获得 horizontal/vertical 运动后添加以下内容:
if (x != 0f || y != 0f) {
Camera.main.orthographicSize = Mathf.MoveTowards (Camera.main.orthographicSize, largeSize, Time.deltaTime);
} else {
Camera.main.orthographicSize = Mathf.MoveTowards (Camera.main.orthographicSize, size, Time.deltaTime);
}
考虑声明 size、largeSize 变量并根据需要进行设置。