Unity2D中如何延迟单行代码?

问题描述 投票:0回答:1

我想将“销毁”的代码行延迟 2 秒。

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

public class enemyDestroyer : MonoBehaviour
{
    private float bounceSpeed = 13f;
    private void OnTriggerEnter2D(Collider2D collision){
        if (collision.gameObject.CompareTag("Enemy")){
            GetComponent<Rigidbody2D>().velocity = Vector2.up * bounceSpeed;
            
            
            Destroy(collision.gameObject);
        }

    }
}
unity-game-engine c++-cli
1个回答
0
投票

应该是这样的。

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

public class enemyDestroyer : MonoBehaviour
{
    private float bounceSpeed = 13f;
    private void OnTriggerEnter2D(Collider2D collision){
        if (collision.gameObject.CompareTag("Enemy")){
            GetComponent<Rigidbody2D>().velocity = Vector2.up * bounceSpeed;
            
            
            Destroy(collision.gameObject, 2f);
        }

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