如何将游戏对象设置为非活动状态,然后在统一几秒钟后处于活动状态?

问题描述 投票:-1回答:3

当玩家与游戏对象碰撞时,我希望使游戏对象处于非活动状态(我已完成此操作)。现在我要等待几秒钟,然后再次激活它。我该怎么办?

这是我的代码

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

}
}
c# unity3d
3个回答
1
投票

我将为偏移时间创建一个公共浮动,然后您可以为计时器创建一个私人浮动。

用于设置对象活动状态的代码可能看起来像这样:

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

}

1
投票

创建像这样的简单的wait通用实用程序:

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

0
投票

[启用新功能以启用/禁用游戏对象。然后在start方法中反复调用最近创建的函数。

© www.soinside.com 2019 - 2024. All rights reserved.