[英]Make the game wait for seconds
我正在使用 c# 代碼在 Unity 中的 Freddy 游戲中創建一種五夜之夜。 我的想法是讓電子動畫嚇跑玩家,然后等待大約 3 秒,最后讓玩家回到主菜單。 我怎樣才能讓游戲(或代碼)在進入菜單之前等待 3 秒? 謝謝
從官方文檔中查看這個示例
https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html
using UnityEngine;
using System.Collections.Generic;
public class ExampleScript : MonoBehaviour
{
// Launches a projectile in 2 seconds
Rigidbody projectile;
void Start()
{
Invoke("LaunchProjectile", 2.0f);
}
void LaunchProjectile()
{
Rigidbody instance = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere * 5.0f;
}
}
所以你可以在你的類中添加一個 Restart 方法,並在你的 jumpscare 函數中延遲 3 秒來調用它
void JumpScare()
{
Invoke("Restart", 3.0f);
}
void Restart()
{
// don't forget using UnityEngine.SceneManagement; at top of your file
SceneManager.LoadScene(0); // index of your scene in builds settings
}
您可以在用於將播放器帶到主菜單的功能上使用 async await。 您需要添加命名空間“使用 System.Threading.Tasks”。 這是一個示例代碼
using UnityEngine;
using System.Threading.Tasks;
public class Play_audio : MonoBehaviour
{
async void Start()
{
await Task.Delay(1000)
Your_fncton();
}
}
來源: https ://vionixstudio.com/2021/11/01/unity-async-await/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.