1、新建c#腳本如下
代碼,在前方生成生成自身圖片并3s后銷毀自身,在碰撞物體后小鳥死亡后不刪除自身
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CopyScene : MonoBehaviour { //要復制的對象public GameObject copyObject;//生成在簽發的距離public float front;private float time = 0f;private bool isDestory = false;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){if (isDestory&&Fly.life) { time += Time.deltaTime;if (time > 3){//3s后銷毀自身Destroy(transform.gameObject);}}}//當某個剛體觸碰到了觸發器private void OnTriggerEnter2D(Collider2D collision){//在前方復制物對象Instantiate(copyObject,new Vector3(transform.position.x+front,transform.position.y,transform.position.z),transform.rotation);isDestory = true;}
}
在背景圖和地面下創建一個2D盒裝碰撞器并勾選是觸發器
按照2點擊是觸發器,單小鳥移動到觸發器位置,則會觸發腳本
按照3調整觸發器大小及位置,調整后就會看到4觸發器的大小