user2852142
user2852142

Reputation:

Unity 3D Object Constantly Cloning

This is unity 2d angry bird style game example. I working on resetter codes. Resetter code below (I changed some parts). I using ball on this project instead of birds. So, I want to if ball is stoped after to throw load new ball to catapult. But It doesn't work correctly. First of all. I wrote if ball count == 0 clone the object. But its alwaws clonning when ball was stoped. On the other hand I cant use cloned object for throwing. Cloned object not on the catapult. Sorry for my bad english but this problem my last phase on this game.

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

This is game tutorial link. 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;
}
}

Upvotes: 0

Views: 456

Answers (1)

Aizen
Aizen

Reputation: 1825

Try this.

void Update(){

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


                  }
        }   
}

Upvotes: 1

Related Questions