Unity:为滑块添加额外时间

时间:2018-05-20 12:31:40

标签: slider unityscript

所以我正在进行这个门游戏,你必须回答一个谜语才能通过门。我在游戏中添加了一个倒计时滑块,现在我想在解决谜语时给予玩家额外的时间。

我怎么能说,当有人在我的谜语上回答正确时,倒计时会增加20秒。 谢谢你的帮助!

我有两个脚本。第一个是门和谜语:

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;

void Awake()
{
sliderChanger = GameObject.FindObjectOfType<Slidertid>();
}

[SerializeField]
private InputField input;

[SerializeField]
private Text text;

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())
{
text.text = "You Guessed Correctly";
NextButton.gameObject.SetActive(true);
GetComponent<Animator>().SetTrigger("DoorATrigger");
input.gameObject.SetActive(false);
door.GetComponent<BoxCollider>().enabled = false;
}
else if (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;
}
else
{
text.text = "Wrong! Try look around for clues";
}
}
}

下一个是滑块:

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;
}

1 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点。适合您提供的代码可以使用这个简单的解决方案。 把你的时间改为公开。

public float time = 50f;

在start方法中,将滑块最大值设置为变量值。

slider.maxValue = time;

然后在做出正确猜测后,加上20秒的时间

sliderChanger.time += 20f;