首页 > 解决方案 > 调用 UI 游戏无法正常运行?

问题描述

所以我有一个用于隐身游戏的敌人守卫脚本,如果发现玩家显示游戏结束状态,则会进行检查。(游戏在 UI 上的文本)。

void Start()
{     
    if(player == null)
    {
        GameObject.FindGameObjectWithTag("Player");
    }
    viewAngle = spotlight.spotAngle;
    originalSpotlighyColour = spotlight.color;

    Vector3[] waypoints = new Vector3[pathHolder.childCount];
    for (int i = 0; i < waypoints.Length; i++)
    {
        waypoints [i] = pathHolder.GetChild (i).position;
        waypoints [i] = new Vector3 (waypoints [i].x, transform.position.y, waypoints [i].z);
    }

    StartCoroutine (FollowPath (waypoints));

}

void Update()
{
    if (CanSeePlayer())
    {
        playerVisibleTimer += Time.deltaTime;
       // spotlight.color = Color.red;
    } else
    {
        playerVisibleTimer -= Time.deltaTime;
       // spotlight.color = originalSpotlighyColour;
    }
    playerVisibleTimer = Mathf.Clamp(playerVisibleTimer, 0, timeToSpotPlayer);
    spotlight.color = Color.Lerp(originalSpotlighyColour, Color.red, playerVisibleTimer / timeToSpotPlayer);

    if(playerVisibleTimer>= timeToSpotPlayer)
    {
        if (OnGuardHasSpottedPlayer != null)
        {
            OnGuardHasSpottedPlayer();
            StopAllCoroutines();
            Seek();
            GameUI.gameIsOver = true;
        }
    }
}

条件很简单:

如果看到玩家,则出现游戏结束图像,玩家将按空格键重新启动阶段。但是 UI 上的图像只显示一次,游戏结束功能不会再次调用。只是在玩家失去关卡时第一次调用,之后你可以进入敌人的视野,什么都不会发生。

标签: unity3d

解决方案


推荐阅读