Unity 3D对象不断克隆

时间:2015-04-17 03:37:30

标签: c# git unity3d

这是团结2d愤怒的小鸟风格游戏的例子。我正在研究重置器代码。下面的重置器代码(我更改了一些部分)。我在这个项目上使用球而不是鸟。所以,我想如果球被停止后将新球投入弹射器。但它没有正常工作。首先。我写的如果球数== 0克隆对象。但是当球被停止时,它的咆哮声仍然存在。另一方面,我不能使用克隆对象进行投掷。克隆的对象不在弹射器上。抱歉我的英语不好,但这个问题我的上一次  这个游戏的阶段。

例如,

https://www.dropbox.com/s/h3gwinw8chyeh59/error.png?dl=0

这是游戏教程链接。 http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/making-angry-birds-style-game

using UnityEngine;
using System.Collections;

public class Resetter : MonoBehaviour {

public Rigidbody2D projectile;          //  The rigidbody of the projectile
public float resetSpeed = 0.025f;       //  The angular velocity threshold of the projectile, below which our game will reset

private float resetSpeedSqr;            //  The square value of Reset Speed, for efficient calculation
private SpringJoint2D spring;           //  The SpringJoint2D component which is destroyed when the projectile is launched

public int BallCount;              // I using this value for  I need new ball or not
public Rigidbody2D  projectileSD; // Ball 





void Start ()
{
    BallCount = 1;

    //  Calculate the Resset Speed Squared from the Reset Speed
    resetSpeedSqr = resetSpeed * resetSpeed;

    //  Get the SpringJoint2D component through our reference to the GameObject's Rigidbody
    spring = projectile.GetComponent <SpringJoint2D>();
}

void Update () {


    //  If the spring had been destroyed (indicating we have launched the projectile) and our projectile's velocity is below the threshold...
    if (spring == null && projectile.velocity.sqrMagnitude < resetSpeedSqr) {
        //  ... call the Reset() function
    //  Reset ();



        if (BallCount == 0 ); 
        {
            ObjeyiKlonla();


        }



    }
}

void ObjeyiKlonla() {              // Clone object






                    Rigidbody2D clone;
                    clone = Instantiate (projectileSD, transform.position, transform.rotation) as Rigidbody2D;
                    clone.velocity = transform.TransformDirection (Vector3.forward * 1);
        BallCount ++;



}

void OnTriggerExit2D (Collider2D other) {
    //  If the projectile leaves the Collider2D boundary...
    if (other.rigidbody2D == projectile) {
        //  ... call the Reset() function
        //Reset ();
    }
}

void Reset () {
    //  The reset function will Reset the game by reloading the same level
    //Application.LoadLevel (Application.loadedLevel);
    BallCount =0;
}
}

1 个答案:

答案 0 :(得分:1)

试试这个。

void Update(){

  if(projectile.GetComponent <SpringJoint2D>() && projectile.velocity.sqrMagnitude < resetSpeedSqr)
        {
                if (BallCount <= 0 ); 
                  {
                          ObjeyiKlonla();


                  }
        }   
}