代码部分(test1):
using System.Collections;
using UnityEngine;
public class test1 : MonoBehaviour
{
public GameObject swan;
private int faceLeft;
private Vector3 pos;
void Start()
{
StartCoroutine(Spawn());
}
IEnumerator Spawn()
{
if (Random.Range(0, 2)==1)
{
faceLeft = 1;
pos = new Vector2(7, Random.Range(-1, 4));
}
else
{
faceLeft = -1;
pos = new Vector2(-7, Random.Range(-1, 4));
}
GameObject instance = Instantiate(swan, pos, Quaternion.identity);//实例化
instance.transform.localScale = new Vector3(1 * faceLeft, 1, 1);//镜像
Rigidbody2D r = instance.GetComponent<Rigidbody2D>();
r.velocity = new Vector3(-5 * faceLeft, 0);//方向
Destroy(instance,7f);
yield return new WaitForSeconds(1);
while (instance != null)
{
yield return 0;
}
StartCoroutine(Spawn());
}
// Update is called once per frame
void Update()
{
}
}