我正在制作一款机制与《愤怒的小鸟》中的蓝鸟类似的游戏(分成3只,克隆的轨迹与最初的方向和速度相同)。我如何将速度应用到相同方向的其他克隆体上?
_spawnpoint1edit.Set(0,+2,0);
_spawnpoint2edit.Set(0,0,0);
_spawnpoint3edit.Set(0,-2,0);
if (Input.GetKeyDown(KeyCode.Space) &&
GetComponent<Rigidbody2D>().velocity.magnitude > 2 &&
_activated == false)
{
_activated = true;
Debug.Log("Ability Activated");
_spawnpoint1 = transform.position + _spawnpoint1edit;
_spawnpoint2 = transform.position + _spawnpoint2edit;
_spawnpoint3 = transform.position + _spawnpoint3edit;
var clone1 = Instantiate (_prefab1, transform.position +
_spawnpoint1edit , Quaternion.identity);
var clone2 = Instantiate (_prefab2, transform.position +
_spawnpoint2edit , Quaternion.identity);
var clone3 = Instantiate (_prefab3, transform.position +
_spawnpoint3edit , Quaternion.identity);
我仍然想写一些代码来删除最初的鸟,所以我最终会得到3个克隆。我需要帮忙把鸟的初始速度传递给这3个克隆体。
我想把它保存在一个变量中,然后把它应用到克隆中,但是我想不出一个方法来解决这个问题。
这是你要找的代码:
public class Test : MonoBehaviour
{
public GameObject prefab;
public void Split()
{
float offset = 1;
Rigidbody2D rb = this.GetComponent<Rigidbody2D>();
GameObject clone1 = Instantiate(prefab, transform.position + Vector3.up * offset, transform.rotation);
GameObject clone2 = Instantiate(prefab, transform.position, transform.rotation);
GameObject clone3 = Instantiate(prefab, transform.position + Vector3.up * offset * -1, transform.rotation);
clone1.GetComponent<Rigidbody2D>().velocity = rb.velocity;
clone2.GetComponent<Rigidbody2D>().velocity = rb.velocity;
clone3.GetComponent<Rigidbody2D>().velocity = rb.velocity;
}
}
当你实例化一个对象时,它会复制它,而不仅仅是将它插入游戏中,你只能拥有一个预制件并多次使用它。我希望这能解决你的问题。