You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity:解谜门游戏中答对谜题为倒计时Slider加20秒的实现

How to Add 20 Seconds to Countdown on Correct Riddle Answer

Got it, let's get this working step by step. Here's what you need to modify in your two scripts to add 20 seconds to the countdown when the player solves the riddle correctly:

Step 1: Fix & Update the SliderChanger Script

First, your current SliderChanger code is missing closing braces for the Update method and the class itself. We'll fix that, plus add a public method to handle adding extra time to the countdown.

Modified SliderChanger script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Characters.FirstPerson;

public class SliderChanger : MonoBehaviour
{
    public Slider slider;
    private float time = 50f;
    public Transform Player;
    public GameObject gameOverUI;

    void Start()
    {
        slider.maxValue = 50f;
        slider.minValue = 0f;
    }

    void Update()
    {
        time -= Time.deltaTime;
        slider.value = time;

        if (slider.value <= 0)
        {
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
            gameOverUI.SetActive(true);
            Player.GetComponent<FirstPersonController>().enabled = false;
        }
    }

    // Public method to add extra time to the countdown
    public void AddExtraTime(float extraSeconds)
    {
        time += extraSeconds;
        // Optional: Uncomment below if you want to cap time at initial max + extra (prevents infinite time)
        // time = Mathf.Min(time, slider.maxValue + extraSeconds);
        slider.value = time; // Immediately update the slider to reflect the new time
    }
}

Step 2: Update the cakeriddle Script

Next, we'll ensure the cakeriddle script correctly references the SliderChanger component, then call the new AddExtraTime method when the player guesses correctly.

Modified cakeriddle script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Characters.FirstPerson;

public class cakeriddle : MonoBehaviour
{
    public GameObject door;
    public GameObject NextButton;
    public GameObject riddleUI;
    public Transform Player;
    private string answer;
    private string answerTwo;
    private string guess;

    // Add this variable to reference the SliderChanger component
    private SliderChanger sliderChanger;

    [SerializeField] private InputField input;
    [SerializeField] private Text text;

    void Awake()
    {
        // Find the SliderChanger component in the scene
        sliderChanger = GameObject.FindObjectOfType<SliderChanger>();
    }

    void OnTriggerEnter(Collider other)
    {
        Cursor.visible = true;
        Cursor.lockState = CursorLockMode.None;
        riddleUI.SetActive(true);
        answer = "cupcake";
        answerTwo = "worldcup";
        text.text = "What kind of cup can’t hold water?";
        Player.GetComponent<FirstPersonController>().enabled = false;
    }

    public void GetInput(string guess)
    {
        CompareGuesses(guess);
        input.text = "";
    }

    public void Next()
    {
        Time.timeScale = 1f;
        Player.GetComponent<FirstPersonController>().enabled = true;
        Cursor.visible = false;
    }

    void CompareGuesses(string guess)
    {
        if (guess.ToLower() == answer.ToLower() || guess.ToLower() == answerTwo.ToLower())
        {
            text.text = "You Guessed Correctly";
            NextButton.gameObject.SetActive(true);
            GetComponent<Animator>().SetTrigger("DoorATrigger");
            input.gameObject.SetActive(false);
            door.GetComponent<BoxCollider>().enabled = false;

            // Call the method to add 20 seconds to the countdown
            sliderChanger.AddExtraTime(20f);
        }
        else
        {
            text.text = "Wrong! Try look around for clues";
        }
    }
}

Key Changes Explained

  • In SliderChanger, we added the AddExtraTime public method that modifies the time variable and updates the slider's value immediately to show the change.
  • In cakeriddle, we properly declared and initialized the sliderChanger variable to connect to the countdown script.
  • We added the call to sliderChanger.AddExtraTime(20f) inside the correct guess condition, so the countdown gets extended as soon as the player solves the riddle.

That's all! Now when the player enters the correct answer, the countdown will jump up by 20 seconds.

内容的提问来源于stack exchange,提问作者Christopher Madsen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:15:22