How to destroy an object after collision (more coplicated)

Hello :slight_smile:

My problem is:

I am making a stacker type of game in which object appears in the top-center of the screen, then player must catch it and put it on the platform (not throw it). I want that object to be destroyed by colliding with the platform, if player will not catch it (using mouse or a finger). However, even after catching it, I want that object to be destroyed if it will fall from high. Now the player can destroy it by hitting Space key, but I want it the way I just explained.

There are 2 prefabs:

  • the object itself
  • fragments of that object (after destroying)

Script that destroys this object (named “example” here) to fragments:

#pragma strict

    var examplePrefab : GameObject;
    var examplePos : Vector3;
    var fragmentLocation : Vector3;

function Update () {


    examplePos = this.gameObject.transform.position;
    fragmentLocation = new Vector3(examplePos.x + 0.7f, examplePos.y + 0.7f, examplePos.z);
    if(Input.GetKeyDown(KeyCode.Space)){
    Destroy(this.gameObject);
    Instantiate(examplePrefab, fragmentLocation, examplePrefab.transform.rotation);
    }
   
   
}

I would be grateful for any help.

Which part are you having trouble with? Or did you not really try yet?

Start at line 12. Instead of checking for the space bar to be pressed, check for a collision with the platform, or a collision on anything at too high a velocity. Then run lines 13 and 14. When you think you have the right code but it’s not working, post that code and you’ll get more specific advice about what to do.

2 Likes