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触发器的大小