[英]How do I play an animation for twenty-seven seconds or more and load a scene in unity3d
Right now I have the script set to switch scenes when the players health gets low. 现在,我将脚本设置为在玩家健康状况下降时切换场景。 I want is the die animation to play first than scene load after twenty-eight seconds .
我想要的是在28秒后首先播放死动画而不是场景加载。 I am going to trigger the die animation .
我要触发死动画。 The die animation clip is 26.0 .
模具动画剪辑为26.0。 The load scene the going to be gameover scene .
负载场景将是游戏结束场景。 The gameover scene needs to load 28.0 or 29.0 .
游戏结束场景需要加载28.0或29.0。 Somewhere around their .
他们周围的某个地方。 Here is my code :
这是我的代码:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
[System.Serializable]
[RequireComponent(typeof(SpriteDatabase))]
public class Healthbar : MonoBehaviour {
public int fontSize;
public static int playersHealth;
public int health;
int healthNormalized;
GameObject player;
Image frame;
Image bar;
public int displayCritical;
public int displayRedBar;
public int displayYellowBar;
public string healthMessage;
public string criticalMessage = "Critical";
public string playerTag;
Text Message;
Text Critical;
public bool showHealthValue;
public bool showCritical;
public string sceneToLoad = "T";
SpriteDatabase sd;
public Theme chosenTheme;
public FontNames chosenFont;
int myTheme;
int myFontTheme;
public enum Positioning {
TopLeft,
TopRight,
BottomLeft,
BottomRight
}
[HideInInspector]
public bool alive = true;
//For demo purposes, store player's initial transform (so later it can be respawned there)
Vector3 startPos;
//used to choose between left or right alignment
public Positioning positioning;
//On Start, assign SpriteDatabse class to 'sd'. (Note: That class can never be missing due to the dependency system)
//It then runs Debugger() (find it below.) It checks whether the required sprites are assigned in the inspector, etc.
//Then, it builds hierarchy for GUI (find below)
void Start(){
sd = GetComponent<SpriteDatabase>();
fontSize = Mathf.Clamp(fontSize, 5, 30);
Debugger();
BuildHierarchy();
startPos = player.transform.position;
}
//Converts health integer to float value and updates it every frame.
//Keeps the GUI bar (image) fill amount value synchronized with the health value.
//Note: healthNormalized cuts the number so that it's on a 100 scale like in every game (it's basically the percentage)
void FixedUpdate(){
if (player) {
if (alive) {
/*
if (healthNormalized <= 0) {
alive = false;
die();
}
*/
healthNormalized = health/10;
//Converts health value to a float (range 0-1) so it can be used for image.fillamount
float healthValue = health * 0.001f;
healthValue = Mathf.Clamp(healthValue, 0, 1);
//Checks if it's time to turn the bar color to red or yellow (replace the sprite basically)
CheckForBarColor();
bar.fillAmount = healthValue;
}
DisplayText();
}
else
player = GameObject.FindGameObjectWithTag("Player");
}
void DisplayText(){
if (showHealthValue)
Message.text = healthMessage + ": " + healthNormalized.ToString();
if (healthNormalized <= displayCritical && alive && showCritical) {
Critical.enabled = true;
}
else
Critical.enabled = false;
}
//Called by every object affecting player's health.
//Class that calls it: ApplyDamage
//See that for more info on how to use it!
public void ModifyHealth(int amount) {
if (alive)
health = health - amount;
if (health <= 0) {
Debug.Log("1: sceneToLoad = " + sceneToLoad);
if ((sceneToLoad != "") && (SceneManager.GetSceneByName(sceneToLoad) != null)) {
Debug.Log("2: sceneToLoad = " + sceneToLoad);
SceneManager.LoadScene(sceneToLoad);
}
}
else {
health = Mathf.Clamp(health, 0, 1000);
}
}
}
Put SceneManager.LoadScene(sceneToLoad);
把
SceneManager.LoadScene(sceneToLoad);
in another function then call that function with Invoke("myfunction",25);
在另一个函数中,然后使用
Invoke("myfunction",25);
调用该函数Invoke("myfunction",25);
. 。 It will wait
25
seconds then call myfunction
which will then load your scene by calling SceneManager.LoadScene(sceneToLoad);
它将等待
25
秒,然后调用myfunction
,然后将通过调用SceneManager.LoadScene(sceneToLoad);
加载场景SceneManager.LoadScene(sceneToLoad);
. 。
You can also start a coroutine and wait with yield return new WaitForSeconds(25f);
您也可以启动协程并等待
yield return new WaitForSeconds(25f);
then execute SceneManager.LoadScene
. 然后执行
SceneManager.LoadScene
。
As for your code, replace the ModifyHealth function with the function below: 对于您的代码,将ModifyHealth函数替换为以下函数:
public void ModifyHealth(int amount)
{
if (alive)
health = health - amount;
if (health <= 0)
{
Debug.Log("1: sceneToLoad = " + sceneToLoad);
if ((sceneToLoad != "") && (SceneManager.GetSceneByName(sceneToLoad) != null))
{
Debug.Log("2: sceneToLoad = " + sceneToLoad);
//Play your animation
//Call loadNewScene after 25 seconds
Invoke("loadNewScene",25);
}
}
else
{
health = Mathf.Clamp(health, 0, 1000);
}
}
void loadNewScene()
{
SceneManager.LoadScene(sceneToLoad);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.