Collisions between clones, text/numbers, spawn control.

Hello. I’ve been going through search after search on this site looking for anything that would help me with my problem, could be I don’t know exactly what to look for but I haven’t found much. I’m working on a “defense” style game and have run into a few speed bumps, first the script I wrote to destroy these spawned meteors doesn’t work. I did find a similar question and the answer was to target the Clone object, not really sure how to do that…


    function OnCollisionEnter(onHit : Collision)
    {
        if(onHit.gameObject.name == "meteorPrefab(Clone)")
           {
              Destroy(gameObject.name("meteorPrefab(Clone)");
           }
    }

This code is the best I’ve been able to come up with from tutorials and such. Does it matter that I’ve got eight spawn points all spawning the meteorPrefab object at the same time and the method I’m trying to use will end up destroying all eight instantiated meteorPrefabs?

Also trying to create a HUD like element. The idea is basically a floating name above the meteor, with with a mix of numbers and letters. The game is played from a top-down perspective and thus I thought a billboard with a lookAt script would be unnecessarily call heavy so I opted for a simple plane level on the X,Z axes. However the play stays with the meteors local Z+ axis and I would like to clamp it to the world Z+, and lock the rotations so no matter how the meteor moves/rotates the plane stays above it on the screen. The plane has a sort of targeting reticule texture on it and I would like the text/numbers to be displayed over this (another plane layered on top?).

As for the spawn control part of the title, I want only one meteor to be spawned from any one spawn point at a time, being that they’re moving in a single direction I figured I’d use raycasting to just check if the meteor object is there, and once it’s not spawn a new one. Heh, testing this idea has to wait til after I can destroy the meteors. Thanks in advance and sorry for the wordy post.

Would using tags (and .tag, instead of .name) work better for you? I don’t know your circumstances and how cumbersome that could make the programming but that might be something you want to look into. (Giving the meteors a meteor tag, or a tag they can share with something else, and checking to see if the collision object has the tag).

I’m not sure I see a question regarding the floating health bar. Maybe you can make it a Cube with a rigidbody and lock it’s rotation in the rigidbody constraints?

Spawning: I’m actually working on a top down game with a ton of spawning and I’m going to very much advise against ray casting for spawning. There are a lot of ways to do spawning but the easiest would probably just to keep track of spawning in a global variable and to check it occasionally when spawning.

There are a dozen ways to do this and I’m not going to reinvent the wheel when I myself started my project with the help of many other answered questions here. Spawning has been answered pretty heavily (especially random spawning).

1- Comparing clone names is not a good practice. It would be better to tag the meteor prefab as “meteor” and check the tag in OnCollisionEnter. The way to destroy the hit object is using onHit.transform.gameObject, like below:

 function OnCollisionEnter(onHit : Collision){
   if(onHit.gameObject.tag == "meteor"){
     Destroy(onHit.transform.gameObject);  // that's the way to kill the hit object 
   }
 }

Don’t mind about the other meteors, only the one which hit the collider will be destroyed.

2- Maybe you should use a GUIText to follow the meteor. There’s a script from @Eric5h5 (ObjectLabel.js, click here to get it) which associates the GUIText to some object, and also includes an offset parameter to keep the text at a fixed distance. You can create a GUIText, adjust font, size, style, anchor etc, add Eric’s script to it and save as a prefab. When instantiating the meteor, instantiate also the GUIText and set its target variable to the meteor - more on this below.

3- There’s an easiest way to avoid launching a meteor while the last one is still alive: child the meteor to the spawning point object right after instantiation and verify transform.childCount - it will count the child meteor while it’s alive, and return to zero when it’s destroyed. The script below (to be attached to the spawning point object) only creates a new meteor when there’s no child. Additionally, it creates the text and associates the meteor to it.

  if (transform.childCount == 0){ // only create a meteor if the previous was destroyed
    // create the meteor
    var meteor: Transform = Instantiate(meteorPrefab,transform.position,transform.rotation);
    meteor.parent = transform; // child the meteor to the spawn point object
    // create the text to follow the meteor
    var text: Transform = Instantiate(textPrefab,Vector3.zero,Quaternion.identity);
    text.GetComponent(ObjectLabel).target = meteor; // associate the meteor to the label
  }