Unity:解谜门游戏中答对谜题为倒计时Slider加20秒的实现
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 theAddExtraTimepublic method that modifies thetimevariable and updates the slider's value immediately to show the change. - In
cakeriddle, we properly declared and initialized thesliderChangervariable 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

