如何制作以便播放器在重播时重新激活?

时间:2017-11-10 02:44:27

标签: c# unity3d gameobject replay

我正在为我所在的大学课程制作一个非常简短的C#Unity游戏,并且我创建了一个陷阱脚本,可以在我的播放器上取消激活,其中还包括一个重播按钮。这一切都有效,除非我重播,播放器保持不活动状态。 如何修改我的脚本以使播放器在重放时重新激活? 另外,我上课这是一个初级班,我对此并不擅长。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Trap : MonoBehaviour 
{   
    public GameObject playerExplosion;
    public GameObject gameOverUI;   

    void OnTriggerEnter (Collider other)    
    {       
        if (other.tag == "Player")
        {
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);           
        }       
        other.gameObject.SetActive(false);
        gameObject.SetActive(false);
        gameOverUI.SetActive(true);
        PlayerController.gameOver = false;
    }
}

编辑:这也是重播脚本。它适用于健康栏系统。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ReplayGame : MonoBehaviour
{
    public Transform player;
    public Image uiBar;
    public GameObject GameOverUI;
    public static Vector3 startPosition;
    private float fillAmount;

    void Start()
    {
        startPosition = player.position;
        fillAmount = uiBar.fillAmount;
        GameOverUI.SetActive(false);
    }

    public void Click ()
    {
        PlayerController.gameOver = false;
        player.position = startPosition;
        uiBar.fillAmount = fillAmount;
        GameOverUI.SetActive(false);       
    }   
}

1 个答案:

答案 0 :(得分:0)

您必须重新激活您的播放器,否则您必须实例化一个新播放器。

public void Replay ()
{
    //ReActivate
    myPlayer.gameObject.SetActive(true);

    //or Instnatiate a new
    Instantiate(myPlayer);
}