C# 玩家与该物体碰撞后,我如何再次使其重生?

C# 玩家与该物体碰撞后,我如何再次使其重生?,c#,unity3d,C#,Unity3d,我想在玩家与物体碰撞后立即在一个新的位置使其重生。 现在,我的代码只是在复活时间后生成对象并销毁以前的对象。 unity2d上我的空对象上的代码是 public GameObject point; private Vector2 screenBounds; public float respawnTime = 10f; GameObject star; void Start() { screenBounds = Camera.main.ScreenToWorldPoint(new

我想在玩家与物体碰撞后立即在一个新的位置使其重生。 现在,我的代码只是在复活时间后生成对象并销毁以前的对象。 unity2d上我的空对象上的代码是

    public GameObject point;
private Vector2 screenBounds;
public float respawnTime = 10f;
GameObject star;
void Start()
{
    screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
    spawnStar();
    StartCoroutine(starWave());

}

public void spawnStar()
{
    Debug.Log("Yeah it works");

    star = Instantiate(point) as GameObject;
    star.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x), Random.Range(-screenBounds.y, screenBounds.y));
}


IEnumerator starWave()
{
    while (true)
    {
        yield return new WaitForSeconds(respawnTime);
        Destroy(star);
        spawnStar();


    }
}
对象预置上的脚本是

void OnTriggerEnter2D(Collider2D col)
{


    if (col.gameObject.tag == "Player")
    {
        Debug.Log("this is star collider destory");
        Destroy(this.gameObject);
        





    }
}
产卵器脚本

public GameObject point;
void Start()
{
    spawnStar();
}

public void spawnStar()
{
    Debug.Log("Yeah it works");

    star = Instantiate(point) as GameObject;
}
预制脚本

// how long before our star moves
private timeBeforeMoving = 10f;

// our current time
private currentTimer = 0.0f;

// the screen size
private Vector2 screenBounds;  
   
void Start()
{
    screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
    transform.position = GenerateNewPosition();
}    

private void Update()
{
    // increase the time that has passed
    currentTimer += Time.deltaTime;
    
    // we have reached the timer threshold, so move the star
    if(currentTimer >= timeBeforeMoving)
    {
        MoveOurStar();
    }
}
    
void OnTriggerEnter2D(Collider2D col)
{
    if (col.gameObject.tag == "Player")
    {
        MoveOurStar();
    }
}

private void MoveOurStar()
{
    // reset the timer as we just moved
    currentTimer = 0.0f;
    transform.position = GenerateNewPosition(); 
}

private Vector2 GenerateNewPosition()
{
    return new Vector2(Random.Range(-screenBounds.x, screenBounds.x), Random.Range(-screenBounds.y, screenBounds.y));; 
}
不要销毁它,而是再次运行随机位置代码。如果您不希望对象在与玩家相同的位置生成,可以使用来确定对象将生成的新位置是否仍在玩家的顶部。如果是,只需再次随机选择一个新位置

如果在创建星型脚本时不想将生成代码放在星型脚本中,可以在星型脚本中设置委托回调,以回调生成它的脚本,让它知道需要移动它。或者使生成程序成为一个或甚至实现一个,以便在对象被销毁时触发事件,让管理器知道如何移动该特定对象

如果您只想使用我提供的示例代码段,并且在实现循环转换时遇到问题,请告诉我,我可以提供另一个代码段


编辑:我对代码做了一些修改。现在,产卵器只会产卵一颗星星,而星星在碰撞后会控制它移动的位置。

ILY兄弟,我会试试这个,让你知道。所以我将这个脚本添加到预置中,现在它可以工作了,但有点不正常,当我第一次与预制件碰撞时,它使其重生,但第二次不起作用,但第三次起作用time@JamalAhmad您不需要使用整个代码段,几乎所有内容都与以前相同。我刚刚更改了行
Destroy(this.gameObject)
to
transform.position=new Vector2(Random.Range(-screenBounds.x,screenBounds.x),Random.Range(-screenBounds.y,screenBounds.y))。我这样做了。现在,当我与它碰撞时,它会产生碰撞,但它第二次没有检测到碰撞,但repsawn计时器耗尽,它重新启动一个新的碰撞,碰撞再次工作。我希望你能理解我。简言之,它没有检测到与OnTigger2D中的obj的碰撞,Functinoni还将第一个脚本附加到空对象,第二个脚本附加到预置