use*_*388 3 c# unity-game-engine
我有一个名为Timer.cs的脚本.此脚本连接到某些GUI文本,该文本显示游戏中剩余的时间量.
此脚本附加了一个音频源,选择了我想要的声音.当时钟到零时,文本变为"GAME OVER!" 并且角色控制锁定; 但是,声音没有播放.
我的场景中audio.Play()的所有其他实例都正常工作,当我将音频源设置为"Play On Awake"时,它播放没有问题.可能是什么问题呢?
Using UnityEngine;
using System.Collections;
public class Timer : MonoBehaviour {
public float timer = 300; // set duration time in seconds in the Inspector
public static int sound = 1;
public static int go = 1;
bool isFinishedLevel = false; // while this is false, timer counts down
void Start(){
PlayerController.speed = 8;
PlayerController.jumpHeight = 12;
}
void Update (){
if (!isFinishedLevel) // has the level been completed
{
timer -= Time.deltaTime; // I need timer which from a particular time goes to zero
}
if (timer > 0)
{
guiText.text = timer.ToString();
}
else
{
guiText.text = "GAME OVER!"; // when it goes to the end-0,game ends (shows time text over...)
audio.Play();
int getspeed = PlayerController.speed;
PlayerController.speed = 0;
int getjumpHeight = PlayerController.jumpHeight;
PlayerController.jumpHeight = 0;
}
if (Input.GetKeyDown("r")) // And then i can restart game: pressing restart.
{
Application.LoadLevel(Application.loadedLevel); // reload the same level
}
}
}
Run Code Online (Sandbox Code Playgroud)
鉴于您将其作为Update例程的一部分进行调用,我不得不猜测问题是您反复调用它.也就是说,你每个帧都在调用它timer <= 0.
你不应该Play()多次打电话.或者至少在它正在播放时不会再次出现.一个简单的解决方案就是这样
if(!audio.isPlaying)
{
audio.Play();
}
Run Code Online (Sandbox Code Playgroud)
看看是否能解决您的问题,然后您就可以从中解决问题.
| 归档时间: |
|
| 查看次数: |
5700 次 |
| 最近记录: |