bool isGameOver;
float xMaxValue = 10; // X value (or -X) of Your right/left screen borders
float yMaxValue = 15; // Y value of Your top screen border
float yMinValue = -5;// Y value of Your bottom screen border
private void Update() {
Vector3 playerPos = transform.position;
If (playerPos.x < -xMaxValue || playerPos.x > xMaxValue ||
playerPos.y < yMinValue || playerPos.y > yMaxValue ) {
isGameOver = true;
}
或边界区域上的脚本(选中其collaider组件“Is trigger”复选框):
void OnTriggerEnter2D(Collider2D col) {
// PlayerScriptName - name of script on Your player GameObject
// Or You can compare tags, or use other approaches to check
// is triggered object a player
if (col.gameObject.TryGetComponent<PlayerScriptName>
(out PlayerScriptName playerScript)) {
// In this case field isGameOver must have
// public access level (in PlayerScriptName class):
playerScript.isGameOver = true;
}
}
[SerializeField]
private float fallYThreshold = -10; // Limit y fall
private void Update()
{
if (transform.position.y <= fallYThreshold)
{
GameOver() //Run game over method here too
}
}
2条答案
按热度按时间3j86kqsm1#
我可以提供两种方法来处理这个函数。你可以在循环更新中用最大最小值匹配对象的坐标,或者你可以在屏幕边界之外添加游戏结束触发区域。例如:
或边界区域上的脚本(选中其collaider组件“Is trigger”复选框):
iqjalb3h2#
如果你的意思是它从一个环境中掉下来,然后显示游戏结束。你只需要下面的简单代码。