簡體   English   中英

如何在統一幾秒鍾后將游戲對象設置為非活動狀態然后激活?

[英]how to set gameobject inactive then active after a few seconds in unity?

當玩家與游戲 object 碰撞時,我想讓游戲 object 處於非活動狀態(我已經這樣做了)。 現在我想等待幾秒鍾,然后再次激活它。 我怎樣才能做到這一點?

這是我的代碼

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

public class PlayerMovement: MonoBehaviour
{

private float chubbyScore = 0;
private float coin = 0;
public float speed = 1;
public float jump = 1;

void Update()
{
    Vector3 playerMovement = Vector3.zero;
    playerMovement.x = Input.GetAxis("Horizontal");
    transform.position += playerMovement * speed * Time.deltaTime;
    transform.Translate(Vector3.forward * Time.deltaTime * speed);

    if (Input.GetKeyDown(KeyCode.Space))
    {
        GetComponent<Rigidbody>().velocity = Vector3.up * jump;
    }
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Pick Up"))
    {
        other.gameObject.SetActive(false);
        coin += 1;
        chubbyScore += 50;
    } 

}
}

我會為偏移持續時間創建一個公共浮點數,然后您可以為計時器創建一個私有浮點數。

將對象設置為活動 state 的代碼可能如下所示:

private float chubbyScore = 0;
private float coin = 0;
public float speed = 1;
public float jump = 1;
public float offsetTime = 2f;
private float timer = 0f;
private GameObject collObj;

void Update()
{
    Vector3 playerMovement = Vector3.zero;
    playerMovement.x = Input.GetAxis("Horizontal");
    transform.position += playerMovement * speed * Time.deltaTime;
    transform.Translate(Vector3.forward * Time.deltaTime * speed);

    if (Input.GetKeyDown(KeyCode.Space))
    {
        GetComponent<Rigidbody>().velocity = Vector3.up * jump;
    }

    if(!collObj.active)
    {
        timer += Time.deltaTime;
        if(timer > offsetTime)
        {
            timer = 0f;
            collObj.SetActive(true);
        }
    }
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Pick Up"))
    {
        collObj = other.gameObject;
        collObj.SetActive(false);
        coin += 1;
        chubbyScore += 50;
    }

}

創建一個簡單的等待通用實用程序,如下所示:

public class Waiter : MonoBehaviour
{
    static Waiter instance = null;
    static Waiter Instance
    {
        get
        {
            if (instance == null)
                instance = new GameObject("Waiter").AddComponent<Waiter>();
            return instance;
        }
    }
    private void Awake()
    {
        instance = this;
    }
    private void OnDestroy()
    {
        if (instance == this)
            instance = null;
    }

    IEnumerator WaitRoutine(float duration, System.Action callback)
    {
        yield return new WaitForSeconds(duration);
        callback?.Invoke();
    }

    public static void Wait(float seconds, System.Action callback)
    {
        Instance.StartCoroutine(Instance.WaitRoutine(seconds, callback));
    }
}

這會在需要時自動將自身注入游戲中,您只需要創建腳本,現在在您的OnTriggerEnter

 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.CompareTag("Pick Up"))
     {
         collObj = other.gameObject;
         collObj.SetActive(false);
         Waiter.Wait(3, () =>
         {
             // Just to make sure by the time we're back to activate it, it still exists and wasn't destroyed.
             if (collObj != null)
                collObj.SetActive(true);
         });
         coin += 1;
         chubbyScore += 50;
      }
 }

制作一個新的 function,您可以在其中啟用/禁用游戲 object。 然后在start方法中反復調用最近做的function。

//Enables or Disables the GameObject every 5 seconds
void Start()
{
     InvokeRepeating("Repeat", 0, 5);
}

void Repeat()
{
     //Enable or Disable the GameObject
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM