其他开发人员:)
我为RTS相机编写了一个脚本,可以防止相机离开地形(当玩家使用WASD /箭头键移动相机时,可以在另一个脚本中找到,不包括在下面)。
正如您所看到的,在下面的代码中,我已经硬编码了地形的宽度和高度。我希望能够自动检测地形的宽度和高度。
注意:我是Unity和C#的新手,所以如果您知道答案,请提供完整的代码,而不仅仅是片段,ty:)
public class CameraBounder : MonoBehaviour {
int minXBound = 50;
int maxXBound = 50;
int minZBound = 100;
int maxZBound = 200;
int terrainWidth = 500;
int terrainHeight = 500;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Debug.Log("X: " + transform.position.x + " Z: " + transform.position.z);
if (transform.position.x > terrainWidth - maxXBound) {
transform.position = new Vector3 ((terrainWidth-maxXBound), transform.position.y, transform.position.z);
}
if (transform.position.x < minXBound) {
transform.position = new Vector3 (minXBound, transform.position.y, transform.position.z);
}
if (transform.position.z > terrainHeight - maxZBound) {
transform.position = new Vector3 (transform.position.x, transform.position.y, (terrainHeight-maxZBound));
}
if (transform.position.z < minZBound) {
transform.position = new Vector3 (transform.position.x, transform.position.y, minZBound);
}
}
}
答案 0 :(得分:0)
正如您在评论中所说,您手动生成关卡,因此,一个解决方案是公开Gameobjects并将其坐标用作点。
然后采取这个游戏对象的位置进行计算。
public class CameraBounder : MonoBehaviour {
public GameObject right;
public Gameobject left;
public Gameobject top;
public Gameobject bottom;
float minXBound;
float maxXBound;
float terrainWidth;
int terrainHeight;
// Use this for initialization
void Start () {
minXBound = left.transform.position.x;
maxXBound = right.transform.position.x;
terrainWidth = maxXBound-minXBound;
terrainHeigh = top.transform.position.y - bottom.transform.position.y;
//You get the idea
}
}
在检查器中创建关卡时,您需要在边缘创建空的游戏对象,并通过检查器将公共变量设置为脚本。