我用行星创造了一个游戏。中心是一颗星星。从下面有一个我要运行的星球。之后,我在屏幕上单击,克隆对象被实例化并进入太阳轨道。行星得到父变换,sun.transform。这很好。但是,有时当行星得到父变换时,它会被销毁。
private void OnCollisionEnter2D(Collision2D other) {
if (isFixed)
return;
if (other.transform.tag == "Sun") {
rb.constraints = RigidbodyConstraints2D.FreezeAll;
rb.bodyType = RigidbodyType2D.Static;
transform.parent = Par;
rb.gravityScale = 0;
isFixed = true;
source.PlayOneShot (Hit, 1);
Game.Score += 1;
Game.plusStage += 1;
PlayerPrefs.SetInt ("Score", Game.Score);
PlayerPrefs.Save ();
if (isHit == false) {
rb.AddTorque (10, ForceMode2D.Impulse);
rb.AddForce (new Vector2 (Random.Range(-1.0f, 1.0f), 10f));
transform.parent = Par2;
GameOver = true;
showGm = true;
}
} else if (other.transform.tag == "Comet" || other.transform.tag == "Planet") {
gameObject.transform.parent = Par2;
rb.bodyType = RigidbodyType2D.Dynamic;
Destroy(gameObject.GetComponent<CapsuleCollider2D>());
gameObject.GetComponent<SpriteRenderer> ().sortingOrder = 3;
isHit = false;
rb.constraints = RigidbodyConstraints2D.None;
gameObject.GetComponent<Rigidbody2D> ().constraints = RigidbodyConstraints2D.None;
rb.gravityScale = 5;
gameObject.GetComponent<CapsuleCollider2D> ().isTrigger = true;
rb.AddTorque (10, ForceMode2D.Impulse);
rb.AddForce (new Vector2 (Random.Range(-1.0f, 1.0f), 10f));
GameOver = true;
source.PlayOneShot (Nohit, 1);
NoHit.Play ();
showGm = true;
if (vibro == 1) {
Handheld.Vibrate();
}
}
}