Search Unity

Destroy Clone by a predefined keyboard key

Discussion in 'Scripting' started by wolkeger, Feb 5, 2016.

  1. wolkeger

    wolkeger

    Joined:
    Jan 31, 2016
    Posts:
    8
    How the hell can I destroy the clones, otherwise I would create tons of clones.
    After I created a clone by pressing Keypad Enter I want to destroy the clone with Keypad +.
    Can someone add this particular function?

    Code (CSharp):
    1. using UnityEngine;using System.Collections;
    2. public class PredictionPath : MonoBehaviour {
    3.     // Use this for initialization
    4.     public Transform Cube; //This is the prefab we will be instantiating as the path steps
    5.     Transform[] Cubes; //the array that will hold all the gameobjects that draw the path(path steps)
    6.     public Transform ShootingPoint; //Shooting Point
    7.     GameObject TidyParent; //just an empty game object to instantiate the path steps inside of it to keep things clean
    8.     public Rigidbody Bullet; //the bullet rigidbody
    9.     Vector3 Gravity; //gravity
    10.     public float FrequencyMultiplier; //this will increase or decrease spacing between path steps
    11.     public int Ammount; //ammount of path steps
    12.     public Vector3 InitialVelocity; //the velocity by which you will shoot the projectile
    13.     void Start () {
    14.         Gravity = Physics.gravity; //sets our gravity variable to the defined project's gravity
    15.         Cubes = new Transform[Ammount]; //creates a new array and sets its size to the Ammount variable
    16.         TidyParent = new GameObject ("TidyParent"); //create empty gameobject
    17.         for (int i=0; i<Ammount-1; i++) { //Instantiate Path Steps and save them in the array
    18.             Cubes[i] = Instantiate(Cube,Vector3.zero,Quaternion.identity) as Transform;
    19.             Cubes[i].parent = TidyParent.transform;
    20.         }
    21.     }
    22.     void Update () {
    23.         UpdatePath ();
    24.         if (Input.GetKeyUp (KeyCode.KeypadEnter)) { //Shoots the bullet for demonstration
    25.             Rigidbody Clone = Instantiate(Bullet,ShootingPoint.position,Quaternion.identity) as Rigidbody;
    26.             Clone.AddForce(InitialVelocity,ForceMode.VelocityChange); //THIS IS VERY IMPORTANT you must use ForceMode.VelocityChange because it doesnt depend on the mass unlike ForceMode.Impulse and it adds the velocity instantly, if you use the default forcemode the prediction wont work.
    27.          
    28.         }
    29.  
    30.             }
    31.     void UpdatePath(){
    32.         for (int i = 0; i < Ammount - 1; i++) { //loop through the array and set each path step to its predicted position
    33.             Vector3 PredictedPosition = PlotPath(ShootingPoint.position,InitialVelocity,i * FrequencyMultiplier);
    34.             Cubes[i].position = PredictedPosition;
    35.            
    36.             }
    37.     }
    38.     Vector3 PlotPath(Vector3 InitialPosition, Vector3 InitialVelocity, float TimeStep){
    39.         return InitialPosition + ((InitialVelocity * TimeStep) + (0.5f * Gravity * (TimeStep * TimeStep))); //this is the very simple equation of motion s=ut+1/2at^2 just with Vector3 in 3 dimensions not one dimension as usual
    40.     }
    41. }
     
  2. RickyX

    RickyX

    Joined:
    Jan 16, 2013
    Posts:
    280
    Do you want to destroy only the last cube you instantiated, with some key on the keyboard ?
    If so, the best way would be to store the last clone you instantiated into a value, and then when you want to destroy it you just take the stored value reference and destroy it from there, like so:
    Code (CSharp):
    1.  
    2.  
    3.  
    4. public class PredictionPath : MonoBehaviour {
    5.     // Use this for initialization
    6.     public Transform Cube; //This is the prefab we will be instantiating as the path steps
    7.     Transform[] Cubes; //the array that will hold all the gameobjects that draw the path(path steps)
    8.     public Transform ShootingPoint; //Shooting Point
    9.     GameObject TidyParent; //just an empty game object to instantiate the path steps inside of it to keep things clean
    10.     public Rigidbody Bullet; //the bullet rigidbody
    11.     Vector3 Gravity; //gravity
    12.     public float FrequencyMultiplier; //this will increase or decrease spacing between path steps
    13.     public int Ammount; //ammount of path steps
    14.     public Vector3 InitialVelocity; //the velocity by which you will shoot the projectile
    15.     GameObject lastClone;
    16.     void Start () {
    17.         Gravity = Physics.gravity; //sets our gravity variable to the defined project's gravity
    18.         Cubes = new Transform[Ammount]; //creates a new array and sets its size to the Ammount variable
    19.         TidyParent = new GameObject ("TidyParent"); //create empty gameobject
    20.         for (int i=0; i<Ammount-1; i++) { //Instantiate Path Steps and save them in the array
    21.             Cubes[i] = Instantiate(Cube,Vector3.zero,Quaternion.identity) as Transform;
    22.             Cubes[i].parent = TidyParent.transform;
    23.         }
    24.     }
    25.     void Update () {
    26.         UpdatePath ();
    27.         if (Input.GetKeyUp (KeyCode.KeypadEnter)) { //Shoots the bullet for demonstration
    28.             Rigidbody Clone = Instantiate(Bullet,ShootingPoint.position,Quaternion.identity) as Rigidbody;
    29.             lastClone = Clone.gameObject;
    30.             Clone.AddForce(InitialVelocity,ForceMode.VelocityChange); //THIS IS VERY IMPORTANT you must use ForceMode.VelocityChange because it doesnt depend on the mass unlike ForceMode.Impulse and it adds the velocity instantly, if you use the default forcemode the prediction wont work.
    31.        
    32.         }
    33.         if (Input.GetKeyUp (KeyCode.T))
    34.         {
    35.              Destroy(lastClone);
    36.         }
    37.             }
    38.     void UpdatePath(){
    39.         for (int i = 0; i < Ammount - 1; i++) { //loop through the array and set each path step to its predicted position
    40.             Vector3 PredictedPosition = PlotPath(ShootingPoint.position,InitialVelocity,i * FrequencyMultiplier);
    41.             Cubes[i].position = PredictedPosition;
    42.          
    43.             }
    44.     }
    45.     Vector3 PlotPath(Vector3 InitialPosition, Vector3 InitialVelocity, float TimeStep){
    46.         return InitialPosition + ((InitialVelocity * TimeStep) + (0.5f * Gravity * (TimeStep * TimeStep))); //this is the very simple equation of motion s=ut+1/2at^2 just with Vector3 in 3 dimensions not one dimension as usual
    47.     }
    48. }
    49.  
    50.  
    NOTE: I did not test this, and it's messy becouse i edited it inside the reply, not in a text editor, so sorry about that.
    If you didn't imagine it like so, let me know, i'm not busy at the moment so i feel like helping around forum.
     
  3. wolkeger

    wolkeger

    Joined:
    Jan 31, 2016
    Posts:
    8
    I´ll test it.