2016-09-07 8 views
0

今、プレイヤーの健康状態が低下したときにシーンを切り替えるようにスクリプトが設定されています。私はダイ・アニメーションが28秒後にシーン・ロードよりも最初に再生されることを望みます。私はダイのアニメーションをトリガーするつもりです。ダイアニメーションクリップは26.0です。ロードシーンはゲームオーバーシーンになるだろう。ゲームオーバーシーンは28.0または29.0をロードする必要があります。彼らの周りのどこか。27秒以上アニメーションを再生し、unity3dでシーンをロードするには

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

答えて

1

はその後、別の関数にSceneManager.LoadScene(sceneToLoad);を入れInvoke("myfunction",25);と、その関数を呼び出します。ここに私のコードです。それは25秒を待ってからmyfunctionを呼び出し、SceneManager.LoadScene(sceneToLoad);を呼び出してシーンをロードします。

コルーチンを開始してyield return new WaitForSeconds(25f);で待機し、次にSceneManager.LoadSceneを実行することもできます。あなたのコードについては

、以下の機能を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); 
} 
+0

アニメーションがブレンドツリーの一部です。いいえ、それを働かせることはできませんでした。 – Freddy

関連する問題